日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

hdu 5072 Coprime

發(fā)布時(shí)間:2025/3/21 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hdu 5072 Coprime 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

http://acm.hdu.edu.cn/showproblem.php?pid=5072

題意:給出 n 個(gè)互不相同的數(shù),求滿足以下條件的三元無序組的個(gè)數(shù):要么兩兩互質(zhì)要么兩兩不互質(zhì)。

思路:根據(jù)同色三角形原理求,白書105頁。求它不符合條件的情況數(shù),先對每一個(gè)數(shù)分解質(zhì)因子,然后利用容斥求出與這個(gè)數(shù)不互質(zhì)的數(shù)的個(gè)數(shù),那么不符合條件的的情況數(shù)加上(x-1)*(n-x); ?x為其它數(shù)與這個(gè)數(shù)不互質(zhì)的個(gè)數(shù)。最后總的情況數(shù)減去不符合的情況數(shù),剩下的就是答案。再求多少個(gè)是這個(gè)數(shù)的倍數(shù)的時(shí)候,可以用篩法求出。

1 #include <cstdio> 2 #include <cstring> 3 #include <vector> 4 #include <algorithm> 5 #define maxn 100010 6 using namespace std; 7 8 int t,n; 9 int a[maxn+10]; 10 int num[maxn+10]; 11 int cnt[maxn+10]; 12 vector<int>g; 13 14 int main() 15 { 16 scanf("%d",&t); 17 while(t--) 18 { 19 memset(cnt,0,sizeof(cnt)); 20 memset(num,0,sizeof(num)); 21 scanf("%d",&n); 22 for(int i=0; i<n; i++) 23 { 24 scanf("%d",&a[i]); 25 num[a[i]]++; 26 } 27 for(int i=1; i<=maxn; i++) 28 { 29 for(int j=i; j<=maxn; j+=i) 30 { 31 cnt[i]+=num[j]; 32 } 33 } 34 __int64 ans=0; 35 for(int i=1; i<maxn; i++) 36 { 37 if(num[i]) 38 { 39 int m=i; 40 g.clear(); 41 for(int j=2; j*j<=i; j++) 42 { 43 if(m%j==0) 44 { 45 g.push_back(j); 46 while(m%j==0) m/=j; 47 } 48 } 49 if(m>1) g.push_back(m); 50 int x=(int) g.size(); 51 int cc=0; 52 for(int j=1; j<(1<<x); j++) 53 { 54 int t2=0; 55 int xx=1; 56 for(int k=0; k<x; k++) 57 { 58 if((1<<k)&j) 59 { 60 t2++; 61 xx*=g[k]; 62 } 63 } 64 if(t2%2) cc+=cnt[xx]; 65 else cc-=cnt[xx]; 66 } 67 ans+=(__int64)max(cc-1,0)*(n-cc); 68 } 69 } 70 __int64 t1=(__int64)n*(n-1)*(n-2)/6; 71 printf("%I64d\n",t1-ans/2); 72 } 73 return 0; 74 } View Code

?

轉(zhuǎn)載于:https://www.cnblogs.com/fanminghui/p/4235248.html

總結(jié)

以上是生活随笔為你收集整理的hdu 5072 Coprime的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。