BZOJ 2818——Gcd
生活随笔
收集整理的這篇文章主要介紹了
BZOJ 2818——Gcd
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題意:給定n,求gcd(x,y)==p 的對數,其中(1<=x<y<n)
思路:
求(x, y) = k, 1 <= x, y <= n的對數等于求(x, y) = 1, 1 <= x, y <= n/k的對數!所以,枚舉每個質數p(線性篩素數的方法見:線性時間內篩素數和歐拉函數),然后求(x, y) = 1, 1 <= x, y <= n/p的個數。
那(x, y) = 1的個數如何求呢?其實就是求互質的數的個數。在[1, y]和y互質的數有phi(y)個,如果我們令x < y,那么答案就是sigma(phi(y))。因為x, y是等價的,所以答案*2,又因為(1, 1)只有一對,所以-1。最終答案為sigma(sigma(phi(n/prime[i])) * 2 - 1)。
參考:https://oi.abcdabcd987.com/eight-gcd-problems/
code:
#include <cstdio> #include <iostream> #include <algorithm> #include <set> #include <cmath> #include <vector> using namespace std; using namespace std; typedef long long ll; const int N = 10000005; const ll maxn=1e18+100; int n,len; int p[N],phi[N]; bool vis[N];int main() {scanf("%d",&n);len=0;for (int i=2;i<=n;i++){if (!vis[i]){p[len++]=i;phi[i]=i-1;}for (int j=0;j<len&&i*p[j]<=n;j++){vis[i*p[j]]=1;if (i%p[j]) phi[i*p[j]]=phi[i]*(p[j]-1);else{phi[i*p[j]] = phi[i] * p[j];break; }}}phi[1]=1;for (int i=2;i<=n;i++) phi[i]+=phi[i-1];ll ans=0;for (int i=0;i<len;i++) ans+=phi[n/p[i]]*2-1;printf("%lld",ans); }
總結
以上是生活随笔為你收集整理的BZOJ 2818——Gcd的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: STL中bitset的用法的整理
- 下一篇: 关于资料和总结