Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4249 Accepted Submission(s): 1211
略坑略坑。
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<cstdlib> 5 using namespace std; 6 7 bool Hash[22]; 8 int f[22],len,qlen; 9 __int64 Q[5002]; 10 11 int gcd(int a,int b) 12 { 13 if(a<0)a=-a; 14 if(b<0)b=-b; 15 if(b==0)return a; 16 int r; 17 while(b) 18 { 19 r=a%b; 20 a=b; 21 b=r; 22 } 23 return a; 24 } 25 void solve(__int64 m) 26 { 27 qlen = 0; 28 Q[0]=-1; 29 for(int i=1;i<=len;i++) 30 { 31 int k=qlen; 32 for(int j=0;j<=k;j++) 33 Q[++qlen]=-1*(Q[j]*f[i]/gcd(Q[j],f[i])); 34 } 35 __int64 sum = 0; 36 for(int i=1;i<=qlen;i++) 37 sum = sum+m/Q[i]; 38 printf("%I64d\n",sum); 39 } 40 int main() 41 { 42 int m,x; 43 __int64 n; 44 while(scanf("%I64d%d",&n,&m)>0) 45 { 46 n=n-1; 47 memset(Hash,false,sizeof(Hash)); 48 for(int i=1;i<=m;i++) 49 { 50 scanf("%d",&x); 51 Hash[x]=true; 52 } 53 for(int i=1;i<=20;i++) 54 { 55 if(Hash[i]==true) 56 for(int j=i+i;j<=20;j=j+i) 57 if(Hash[j]==true) Hash[j]=false; 58 } 59 len = 0; 60 for(int i=1;i<=20;i++)if(Hash[i]==true) f[++len]=i; 61 solve(n); 62 } 63 return 0; 64 }