UVa 11426 - GCD - Extreme (II)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2421
代码及其注释:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue> #define ll long long
#define lint long long
using namespace std; const int N=4000005;
int phi[N];//phi[k] 表示从1到k 和k互质的数有几个
ll f[N],sum[N];//f[k] 表示从1到k-1 依次求和k的最大公约数 所以最大公约数的总和
void phin(int n=N-1)
{
memset(phi,0,sizeof(phi));
phi[1]=1;
for(int i=2;i<=n;++i)
if(!phi[i])
{
for(int j=i;j<=n;j=j+i)
{
if(!phi[j]) phi[j]=j;
phi[j]=phi[j]/i*(i-1);
}
}
}
int main()
{
//freopen("data.in","r",stdin);
phin();
memset(f,0,sizeof(f));
memset(sum,0,sizeof(sum));
for(int i=1;i<N;++i)
for(int j=i+i,l=2;j<N;j=j+i,++l)
{
f[j]+=(ll)phi[l]*(ll)i;//这里的phi[l]表示小于j的数中与j的最大公约数是i的数的个数
}
for(int i=1;i<N;++i)
sum[i]=f[i]+sum[i-1];
int k;
while(cin>>k)
{
if(!k) break;
cout<<sum[k]<<endl;
}
return 0;
}
UVa 11426 - GCD - Extreme (II)的更多相关文章
- UVA 11426 - GCD - Extreme (II) (数论)
UVA 11426 - GCD - Extreme (II) 题目链接 题意:给定N.求∑i<=ni=1∑j<nj=1gcd(i,j)的值. 思路:lrj白书上的例题,设f(n) = gc ...
- UVA 11426 GCD - Extreme (II) (欧拉函数)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Problem JGCD Extreme (II)Input: Standard ...
- 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< ...
- 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之和 问题 ...
随机推荐
- Jdbc入门
JDBC入门 l 导jar包:驱动! l 加载驱动类:Class.forName(“类名”); l 给出url.username.password,其中url背下来! l 使用DriverMa ...
- 数位dp——统计'1'的个数
今天去牛客网看了看 包含一 这道题,一开始没看清,以为它要统计 1~n 所有数中数字 '1' 出现的总次数,也就是说,若 n == 11,则 ans = 4:而按照题目的原意 ans 应该为 3.看错 ...
- Linux runlevel 运行级别
runlevel可以认为是系统状态,形象一点,您可以认为runlevel有点象微软的windows操作系统中的Normal,safemode,和Command prompt only. Linux系统 ...
- 转载: C++ 转换构造函数 和 类型转换函数
1.对于系统的预定义基本类型数据,C++提供了两种类型转换方式:隐式类型转换和显式类型转换. ,sum; double b=5.55; sum=a+b;//-------(1) std::cout&l ...
- CentOS查看内核版本,位数,版本号 (zhuan)
http://blog.csdn.net/painsonline/article/details/7668824 ******************************************* ...
- Command设计模式
1 意图:将一个请求封装为一个对象,可以用不同的请求对客户进行参数化: 对请求排队或记录请求日志,以及支持可撤销的操作. 2 别名:Action.Transaction 3 动机:把请求变成一个对象. ...
- [转]Android_气泡效果
最近在看以前在eoe上收藏的一些源代码,准备将这些代码加上一些自己的注释,然后贴出来,方便自己日后查阅,和刚入门的人来学习. 今天先看一个气泡窗口,先看一下效果图和目录结构,然后再上代码 通过第一幅图 ...
- An unknown server-side error occurred while processing the command.处理
在调用resetAPP()时,报错:An unknown server-side error occurred while processing the command. 怎么解决呢?请看: 额,Ap ...
- phonegap插件加载与使用
有朋友问能不能在CanTK和AppBuilder开发的APP里发送UDP数据,HTML5里只能用HTTPS/HTTP/WebSocket几种通讯方式,要使用UDP需要通过phonegap打包成APK等 ...
- 扩展Date的DateDiff方法--日期差
Date.prototype.DateDiff = function(after){ var diffDay; var beforeDate = new Date(this).format(" ...