UVA 11426 - GCD - Extreme (II) (数论)
UVA 11426 - GCD - Extreme (II)
题意:给定N。求∑i<=ni=1∑j<nj=1gcd(i,j)的值。
思路:lrj白书上的例题,设f(n) = gcd(1, n) + gcd(2, n) + ... + gcd(n - 1, n).这种话,就能够得到递推式S(n) = f(2) + f(3) + ... + f(n) ==> S(n) = S(n - 1) + f(n);.
这样问题变成怎样求f(n).设g(n, i),表示满足gcd(x, n) = i的个数,这样f(n) = sum{i * g(n, i)}. 那么问题又转化为怎么求g(n, i),gcd(x, n) = i满足的条件为gcd(x / i, n / i) = 1,因此仅仅要求出欧拉函数phi(n / i),就能够得到与x / i互质的个数,从而求出gcd(x , n) = i的个数,这样总体就能够求解了
代码:
#include <stdio.h>
#include <string.h> const int N = 4000005; int n;
long long phi[N], s[N], f[N]; int main() {
phi[1] = 1;
for (int i = 2; i < N; i++) {
if (phi[i]) continue;
for (int j = i; j < N; j += i) {
if (!phi[j]) phi[j] = j;
phi[j] = phi[j] / i * (i - 1);
}
}
for (int i = 1; i < N; i++) {
for (int j = i * 2; j < N; j += i) {
f[j] += phi[j / i] * i;
}
}
s[2] = f[2];
for (int i = 3; i < N; i++)
s[i] = s[i - 1] + f[i];
while (~scanf("%d", &n) && n) {
printf("%lld\n", s[n]);
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
UVA 11426 - GCD - Extreme (II) (数论)的更多相关文章
- UVA 11426 GCD - Extreme (II) (数论|欧拉函数)
题意:求sum(gcd(i,j),1<=i<j<=n). 思路:首先能够看出能够递推求出ans[n],由于ans[n-1]+f(n),当中f(n)表示小于n的数与n的gcd之和 问题 ...
- UVA 11426 GCD - Extreme (II) (欧拉函数)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Problem JGCD Extreme (II)Input: Standard ...
- UVa 11426 - GCD - Extreme (II)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVA 11426 GCD - Extreme (II) (欧拉函数+筛法)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70017#problem/O 题意是给你n,求所有gcd(i , j)的和,其中 ...
- UVA 11426 GCD - Extreme (II) (欧拉函数)题解
思路: 虽然看到题目就想到了用欧拉函数做,但就是不知道怎么做... 当a b互质时GCD(a,b)= 1,由此我们可以推出GCD(k*a,k*b)= k.设ans[i]是1~i-1与i的GCD之和,所 ...
- UVA 11426 GCD - Extreme (II)(欧拉函数打表 + 规律)
Given the value of N, you will have to find the value of G. The definition of G is given below:Here ...
- uva 11426 GCD - Extreme (II) (欧拉函数打表)
题意:给一个N,和公式 求G(N). 分析:设F(N)= gcd(1,N)+gcd(2,N)+...gcd(N-1,N).则 G(N ) = G(N-1) + F(N). 设满足gcd(x,N) 值为 ...
- UVa 11426 - GCD - Extreme (II) 转化+筛法生成欧拉函数表
<训练指南>p.125 设f[n] = gcd(1, n) + gcd(2, n) + …… + gcd(n - 1, n); 则所求答案为S[n] = f[2]+f[3]+……+f[n] ...
- UVA 11426 - GCD - Extreme (II) 欧拉函数-数学
Given the value of N, you will have to find the value of G. The definition of G is given below:G =i< ...
随机推荐
- 在线maven
仓库
findmaven.net是一个查找Jar和查找Maven的Maven仓库搜索引擎.它能够依据Java开发人员提供的Class名或者Jar名找到包括它的Jar,同一时候提供Jar的Maven仓库链接, ...
- [LeetCode203]Remove Linked List Elements
题目: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 ...
- 使用Simple DNS plus 构建自己的DNS
1.下载并安装Simple DNS plus 2.界面例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2tfYm9zcw==/font/5a6L ...
- UVA 11769 All Souls Night 的三维凸包要求的表面面积
主题链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=2869">点击打开链接 求给定的 ...
- jxl创Excel档java示例代码说明
记得下载 并 导入jxl.jar 包,免积分下载地址:http://download.csdn.net/detail/u010011052/7561041 package Test; import j ...
- 第十二章——SQLServer统计信息(3)——发现过期统计信息并处理
原文:第十二章--SQLServer统计信息(3)--发现过期统计信息并处理 前言: 统计信息是关于谓词中的数据分布的主要信息源,如果不知道具体的数据分布,优化器不能获得预估的数据集,从而不能统计需要 ...
- hdoj 2121 Ice_cream’s world II 【没有最低树的根节点】
称号:pid=2121" target="_blank">hdoj 2121 Ice_cream's world II 题意:题目是一道躶题,给n个点,m条边的有向 ...
- n每个计数的概率和发生骰子--动态规划
称号:该n骰子在地板上.所有点骰子的向上一面和一个S.进入n,打印S所有可能的值的概率. 声明思想非原创!仅仅因动态规划思想的使用非常好,记下. 分析:动态规划就是分阶段考虑问题.给出变量.找出相邻阶 ...
- 一起写2048(160行python代码)
前言: Life is short ,you need python. --Bruce Eckel 我与2048的缘,不是缘于一个玩家.而是一次,一次,重新的ACM比赛.四月份校赛初赛,第一次碰到20 ...
- libsvm工具箱C++编程实践2
转载请注明出处 http://blog.csdn.net/u013491262/article/details/37344193 点击打开链接 上周因为皮肤有点过敏,去医院来来回回一周. 前几天 ...