Evaluate the number of four numbers in a series
Still in reverse consideration, calculate the number of four numbers in the sequence that are not mutually qualitative, And then subtract the total number.
If you want to calculate the number of four numbers that do not have mutual quality, you cannot directly find the result. It is better to find the public factor of 2, which is 3, which is... Number,
Then, based on the review principle, find the value of 2, which is 3 .. And then subtract 2 and 3 to 3 and 5... In addition, the Public factor is 3...
You can.
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define LL long longusing namespace std;const int N = 10010;LL num[N];void init(){ for (int i=4;i<N;i++){ num[i]=(LL)i*(i-1)*(i-2)*(i-3)/24; }}int n;int vis[N],t[N],prime[N],ret;void proc(int x){ ret=0; for (int i=2;i*i<=x;i++){ if (x%i==0){ prime[ret++]=i; while (x%i==0) x/=i; } } if (x>1) prime[ret++]=x; for (int i=1;i<(1<<ret);i++){ int flag=0,tmp=1; for (int j=0;j<ret;j++){ if ((1<<j)&i){ tmp*=prime[j]; flag++; } } vis[tmp]++; t[tmp]=flag; }}int main(){ int a; init(); while (scanf("%d",&n)!=EOF) { memset(vis,0,sizeof vis); memset(t,0,sizeof t); for (int i=0;i<n;i++){ scanf("%d",&a); proc(a); } LL ans=0; for (int i=1;i<N;i++){ if (vis[i]>0){ if (t[i]&1) ans+=num[vis[i]]; else ans-=num[vis[i]]; } } printf("%lld\n",num[n]-ans); } return 0;}
Poj 3904 sky code rejection Principle