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之和 问题 ...
随机推荐
- 使用otl,报错:mysql Commands out of sync; you can't run this command now
1.代码如下: void TestCache(otl_connect& otlConn) { try { ] = {}; sprintf(sql,"call test1(1)&quo ...
- Ubuntu Server14.04 32位安装odoo8.0简单方法
一.wget -O - https://nightly.odoo.com/odoo.key | apt-key add - 二.echo "deb http://nightly.odoo.c ...
- DOM节点操作
请尊重知识,请尊重原创 更多资料参考请见 http://www.cezuwang.com/listFilm?page=1&areaId=906&filmTypeId=1 一.创建节 ...
- 【ufldl tutorial】Convolution and Pooling
卷积的实现: 对于每幅图像,每个filter,首先从W中取出对应的filter: filter = squeeze(W(:,:,filterNum)); 接下来startercode里面将filter ...
- Boostrap学习心得
进入新公司,前端用到各种页面元素几乎都是bootStrap框架里的,下面介绍下某些页面控件怎么运用,作为知识点备份,提供有需要人观看: 首先下载bootstrap,地址http://www.bootc ...
- Mvc4_Area的应用
为什么需要分离? 我们知道MVC项目各部分职责比较清晰,相比较ASP.NET Webform而言,MVC项目的业务逻辑和页面展现较好地分离开来,这样的做法有许多优点,比如可测试,易扩展等等.但是在实际 ...
- 腾讯云从零部署nodejs站点
版权声明:本文由袁飞翔原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/176 来源:腾云阁 https://www.qclo ...
- 本地化Model Factory
即让其生成中文的测试数据 先简单介绍Model Factory两个常用方法: 进入tinker页面生成测试数据: Factory(User::class,10)->make():make()方法 ...
- robot.libdocpkg package
mplements the Libdoc tool. The command line entry point and programmatic interface for Libdoc are pr ...
- VBA中四种自动运行的宏以及模块的含义
在Excel的“标准模块”中可以创建4种自动运行的宏,它们分别是Auto_Open(打开工作 簿时自动运行), Auto_Close, Auto_Activate, Auto_Deactivate. ...