标签:des style blog http color io os ar java
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 56 Accepted Submission(s):
20
先用容斥求出和a[i] 互质的个数num ,然后不符合条件的 就是 num*(n-1-num);
把所有不符合的加起来/2, 就是所有不符合的组数,最后用总的组数减去不合法的就好了
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<vector> #include<set> #include<stack> #include<map> #include<ctime> #include<bitset> #define LL long long #define INF 999999 #define maxn 500010 using namespace std; int cnt[maxn] ,a[maxn] ; vector<int>q; int main() { int i ,j , n ,m , k , v ; LL ans,sum; int T ,len,u; cin >>T ; while(T--) { scanf("%d",&n) ; for( i =1 ; i <= n ;i++) scanf("%d",&a[i]) ; memset(cnt,0,sizeof(cnt)) ; for( i = 1 ; i <= n ;i++) { for( j = 1 ; j*j <= a[i] ;j++)if(a[i]%j==0) { cnt[j]++ ; if(a[i]/j != j )cnt[a[i]/j]++; } } ans = 0 ; for( i = 1 ; i <= n ;i++) { q.clear(); m = a[i]; for( j = 2 ; j*j <= m ;j++)if(m%j==0) { q.push_back(j) ; while(m%j==0) m /= j ; } if(m != 1) q.push_back(m) ; k = q.size() ; len = (1<<k) ; int hehe=0; sum=0; u=1; for( j = 1 ;j < len ;j++) { hehe=0; u=1; for( v = 0 ; v < k ;v++ ) if((1<<v)&j) { hehe++ ; u *= q[v] ; } if(hehe&1) sum += cnt[u] ; else sum -= cnt[u] ; } if(sum)sum--; ans += sum*(n-1-sum) ; } ans/=2; LL hehe = (LL)n*(n-1)*(n-2)/6 ; printf("%I64d\n",hehe-ans) ; } return 0 ; }
标签:des style blog http color io os ar java
原文地址:http://www.cnblogs.com/20120125llcai/p/4044649.html