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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

51nod 1040最大公约数和(欧拉函数)

發布時間:2023/12/10 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 51nod 1040最大公约数和(欧拉函数) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1040?最大公約數之和 題目來源:?rihkddd 基準時間限制:1?秒 空間限制:131072?KB 分值:?80?難度:5級算法題 ?收藏 ?關注 給出一個n,求1-n這n個數,同n的最大公約數的和。比如:n = 6 1,2,3,4,5,6 同6的最大公約數分別為1,2,3,2,1,6,加在一起 = 15 Input 1個數N(N?<=?10^9) Output 公約數之和 Input示例 6 Output示例 15 思路: 目的是求∑(i= 1,n) gcd( i , n ); gcd( i , n ) = x,表示x是n的因子。稍作變形gcd( i / x , n / x) = 1, 看到這個式子可以想到歐拉函數,也就是求比n/x小的與其互質的個數。 因為這些書和n/x互質,乘上x后與n的最大公約數只有x。 也就是說我們先求出每個因子,然后計算每個因子有多少貢獻即可。 /** Author: sweat123* Created Time: 2016/6/27 14:01:46* File Name: main.cpp*/ #include<set> #include<map> #include<queue> #include<stack> #include<cmath> #include<string> #include<vector> #include<cstdio> #include<time.h> #include<cstring> #include<iostream> #include<algorithm> #define INF 1<<30 #define MOD 1000000007 #define ll long long #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define pi acos(-1.0) using namespace std; const int MAXN = 1000000; int n; ll ef(int n) { ll cnt = n; int i; for(i = 2; i * i <= n; i++){ if(n % i == 0) { cnt -= cnt / i; // (x-x/p1) *(1-1/p2)*(1-1/p3)*(1-1/p4).....while(n % i == 0) n /= i; } }if(n > 1) cnt -= cnt / n;return cnt; } int main(){while(~scanf("%d",&n)){ll ans = 0;for(int i = 1; i <= (int)sqrt(n); i++){if(n % i == 0){ans += ef(n / i) * i;if(n / i != i){ans += ef(i) * (n / i); }}} printf("%lld\n",ans);}return 0; }

?

轉載于:https://www.cnblogs.com/sweat123/p/5620086.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的51nod 1040最大公约数和(欧拉函数)的全部內容,希望文章能夠幫你解決所遇到的問題。

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