#include<cstdio> #include<cstdlib> #include<cstring> #include<vector> #include<iostream> #include<cmath> using namespace std; bool mark[33000]; int main() { char a[10000]; memset(mark,true,sizeof(mark)); mark[0]=mark[1]=false; for(int i=4;i<=10000;i=i+2)mark[i]=false; for(int i=3;i*i<=10000;i=i+2) { if(mark[i]) { for(int j=i*i;j<=10000;j=j+2*i ) mark [j] = false; } } vector<int>prime; for(int i=2;i<33000;i++)if(mark[i])prime.push_back(i); while(gets(a)!=NULL) { char *ch; ch=strtok(a," "); int i=atoi(ch); if(i==0)break; ch=strtok(NULL," "); int sum=0; sum=pow(i,atoi(ch)); while(true) { ch=strtok(NULL," "); if(!ch)break; i=atoi(ch); ch=strtok(NULL," "); sum=sum*pow(i,atoi(ch)); } sum=sum-1; int freq[33000]; //printf("%d\n",sum); memset(freq,0,sizeof(freq)); for(i=0;i<prime.size()&&sum>1;i++) { int n=prime[i]; while(sum%n==0) { freq[n]++; sum=sum/n; } } bool flag=false; for(i=33000-1;i>=0;i--) { if(freq[i]>0) { if(flag==true)printf(" "); printf("%d %d",i,freq[i]); flag=true; } }printf("\n"); } return 0; }
Sunday, August 24, 2014
uva - 516 - Prime Land
Subscribe to:
Post Comments (Atom)
explAin it
ReplyDelete