标签:
Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6630 Accepted Submission(s): 1913
#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> #include <stdlib.h> #include <math.h> using namespace std; int gcd(int a,int b){ return b==0?a:gcd(b,a%b); } int lcm(int a,int b){ return a/gcd(a,b)*b; } int main() { int n,m,a[15]; while(scanf("%d%d",&n,&m)!=EOF){ int id = 0,num; for(int i=0;i<m;i++){ scanf("%d",&num); if(num!=0) a[id++] = num; } int ans = 0; for(int i=1;i<(1<<id);i++){ int l=1,cnt=0; for(int j=0;j<id;j++){ if((i>>j)&1){ cnt++; l = lcm(l,a[j]); } } if(cnt&1){ ans+=(n-1)/l; ///不包括自身所以n-1 }else{ ans-=(n-1)/l; } } printf("%d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5539828.html