Question:
Give N and then the number of M. Question 1 ~ The number of N-1 can be divided by one or more of the M number.
Ideas:
First n --
Then, for each number of M actually 1 ~ N-1 can be divided by its internal is (N-1)/m [I]
But there will be duplicates. For example, in Example 6, the calculation will be repeated.
At this time, we need to refresh the principle.
Add the first number minus two numbers ..
Here we need to note that when two or more numbers are used, we need to calculate LCM instead of simply multiplying them!
Code:
#include "stdio.h"#include "string.h"#include "math.h"#include "iostream"#include "cstdlib"#include "algorithm"#include "queue"using namespace std;int a[12];int used[12],b[12];int n,m;int gcd(int a,int b){ return b==0?a:gcd(b,a%b);}int lcm(int k){ int ans=b[0]; for(int i=1;i<k;i++) { int tep=gcd(ans,b[i]); ans=ans/tep*b[i]; } return ans;}__int64 dfs(int kk,int x,int lit){ __int64 ans=0; if(x==lit) { int tep; tep=lcm(x); return n/tep; } for(int i=kk+1;i<m;i++) { if(a[i]==0) continue; if(used[i]) continue; used[i]=1; b[x]=a[i]; ans+=dfs(i,x+1,lit); used[i]=0; } return ans;}int main(){ while(scanf("%d%d",&n,&m)!=-1) { n--; for(int i=0;i<m;i++) scanf("%d",&a[i]); __int64 ans=0; for(int i=1;i<=m;i++) { // printf("%d\n",dfs(-1,0,i)); memset(used,0,sizeof(used)); if(i%2==0) ans-=dfs(-1,0,i); else ans+=dfs(-1,0,i); } printf("%I64d\n",ans); } return 0;}
[Rejection principle] HDU 1796 how many integers can you find