12 2 2 3
7
/* Author:ZXPxx Memory: 1616 KB Time: 202 MS Language: C++ Result: Accepted */ #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <string> #include <vector> #include <cmath> #include <ctime> #include <queue> #include <stack> #include <set> #include <map> using namespace std; typedef long long LL; const int maxn = 5e5 + 5; const int inf = 0x3f3f3f3f; int a[130],n,m,ans,p,cur; int gcd(int a,int b) { return !b ? a:gcd(b,a%b); } int lcm(int a,int b) { return a/gcd(a,b)*b; } void dfs(int i,int flag,int k) { k=lcm(a[i],k); ans+=n/(k*flag); for(int j=i+1;j<cur;j++) dfs(j,-flag,k); } int main() { while(~scanf("%d %d",&n,&m)) { cur=0; for(int i=0; i<m; i++) { scanf("%d",&p); if(p) a[cur++]=p; } ans=0;n--; for(int i=0; i < cur; i++) { dfs(i,1,a[i]); } printf("%d\n",ans); } return 0; }
/* Author:ZXPxx Memory: 1648 KB Time: 998 MS Language: C++ Result: Accepted */ #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <string> #include <vector> #include <cmath> #include <ctime> #include <queue> #include <stack> #include <set> #include <map> using namespace std; typedef long long LL; const int maxn = 5e5 + 5; const int inf = 0x3f3f3f3f; int a[130],m,n,p,cur,ans; int gcd(int a,int b) { return !b ? a:gcd(b,a%b); } int lcm(int a,int b) { return a/gcd(a,b)*b; } int main() { while(~scanf("%d %d",&n,&m)) { n--; cur=0; ans=0; for(int i=0; i<m; i++) { scanf("%d",&p); if(p) a[cur++]=p; } for(int i= 1;i < pow(2,cur); i++) { int cnt=0,flag=0,k=1; for(int j = 0; j < cur; j++) { if(i>>j & 1) { k=lcm(k,a[j]); if(k>n) { flag=1; break; } cnt++; } } if(flag) continue; if(cnt%2) ans+=n/k; else ans-=n/k; } printf("%d\n",ans); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU 1796 How many integers can you find
原文地址:http://blog.csdn.net/zhang_xueping/article/details/47699787