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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

hdu 1695

發布時間:2023/12/9 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hdu 1695 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目:http://acm.hdu.edu.cn/showproblem.php?pid=1695

給出a b c d k 在 [a,b] [c,d]內各找一個數,使得gcd(x,y) == k,也就是說 gcd(x / k, y / k) == 1,所以就是在 [1,b / k] [1, d / k]中找互質的對數。容斥原理和歐拉函數的應用,首先在打印素數的同時求出每個數的歐拉函數值,對于[1,b]內的數直接用歐拉數組求互質的對數,對于[b + 1,d]的數用容斥原理求互質對數,首先求出不互質的對數,然后用總的減去不互質的對數。比如說某個數 n ,首先求出 n 的所有因子num, 假設單個因子個數為num1,任意兩個因子個數相乘所得因子個數num2,任意三個因子相乘所得因子個數num3 ~~~numn,?那么與n 不互質的個數 tnum = num1 - num2 + num3 - num4 + ~~~~ (-1) ^ (n - 1) numn

View Code 1 typedef __int64 ll; 2 const int N = 100001; 3 int prime[N], phi[N]; 4 int num,tnum; 5 void is_prime() // 求歐拉函數 6 { 7 int i,j; 8 phi[0] = 0, phi[1] = 1; 9 for(i = 2; i < N; i++) 10 { 11 if(!phi[i]) 12 { 13 phi[i] = i - 1; 14 prime[num ++] = i; 15 } 16 for(j = 0; (j < num) && (i * prime[j]) < N; j++) 17 { 18 if(i % prime[j] == 0) 19 { 20 phi[i * prime[j]] = phi[i] * prime[j]; 21 break; 22 } 23 phi[i * prime[j]] = phi[i] * (prime[j] - 1); 24 } 25 } 26 } 27 ll solve(int n,int r) // 容斥定理求互質個數 28 { 29 vector<int>p; 30 for(int i = 2; i * i <= n; i++) 31 { 32 if(n % i == 0) p.push_back(i); 33 while(n % i == 0) n /= i; 34 } 35 if(n > 1) p.push_back(n); 36 ll sum = 0; 37 for(int j = 1; j < (1 << p.size()); j ++) 38 { 39 ll mult = 1, bit = 0; 40 for(int i = 0; i < (int)p.size(); i++) 41 { 42 if(j & (1 << i)) 43 { 44 bit ++; 45 mult *= p[i]; 46 } 47 } 48 ll cur = r / mult; 49 if(bit % 2) sum += cur; 50 else sum -= cur; 51 } 52 return r - sum; 53 } 54 int main() 55 { 56 int a,b,c,d,k; 57 int i; 58 int t,cs = 0; 59 is_prime(); 60 //freopen("data.txt","r",stdin); 61 scanf("%d",&t); 62 while(t--) 63 { 64 scanf("%d%d%d%d%d",&a,&b,&c,&d,&k); 65 printf("Case %d: ",++cs); 66 if(k == 0) 67 { 68 printf("%d\n",0); 69 continue; 70 } 71 b /= k, d /= k; 72 if(b > d) swap(b,d); 73 ll ans = 0; 74 for(i = 1; i <= b; i++) 75 ans += phi[i]; 76 for(i = b + 1; i <= d; i++) 77 { 78 ans += solve(i,b); 79 } 80 cout<<ans<<endl; 81 //printf("%I64d\n",ans); 82 //printf("%lld\n",ans); 83 } 84 return 0; 85 }

轉載于:https://www.cnblogs.com/fxh19911107/archive/2012/10/11/2720246.html

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。