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) 值为 i 的且1<=x<=N-1的x的个数为 g(i,N)。 则F(N) = sigma{ i * g(i,N) }。
因为gcd(x,N) == i 等价于 gcd(x/i, N/i) == 1,且满足gcd(x/i , N/i)==1的x的个数就是 N/i 的欧拉函数值。所以g(i,N) 的值 就是phi(N/i)。
打表预处理出每个数的欧拉函数值和每个数对应的答案即可。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 4e6+;
int phi[maxn]; //欧拉函数
LL ans[maxn];
void Euler()
{ //欧拉函数表
for(int i=;i<maxn;++i) phi[i] = i;
for(int i=;i<maxn;++i){
if(phi[i]!=i) continue;
for(int j=i;j<maxn;j+=i)
phi[j] = phi[j] - phi[j]/ i;
}
for(int i=;i<maxn;++i){
for(int j=i+i;j<maxn;j+=i){
ans[j] += i*phi[j/i];
}
}
for(int i=;i<maxn;++i) ans[i]+=ans[i-];
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
Euler();
int N;
while(scanf("%d",&N)==){
if(!N) break;
printf("%lld\n",ans[N]);
}
return ;
}
uva 11426 GCD - Extreme (II) (欧拉函数打表)的更多相关文章
- 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) (欧拉函数+筛法)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70017#problem/O 题意是给你n,求所有gcd(i , j)的和,其中 ...
- 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) 欧拉函数
分析:枚举每个数的贡献,欧拉函数筛法 #include <cstdio> #include <iostream> #include <ctime> #include ...
- UVA 11424 GCD - Extreme (I) (欧拉函数+筛法)
题目:给出n,求gcd(1,2)+gcd(1,3)+gcd(2,3)+gcd(1,4)+gcd(2,4)+gcd(3,4)+...+gcd(1,n)+gcd(2,n)+...+gcd(n-1,n) 此 ...
- UVA11426 GCD - Extreme (II) (欧拉函数/莫比乌斯反演)
UVA11426 GCD - Extreme (II) 题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 10 100 200000 0 输出样例#1: 67 13 ...
- UVA11426 GCD - Extreme (II)---欧拉函数的运用
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA11426 GCD - Extreme (II) —— 欧拉函数
题目链接:https://vjudge.net/problem/UVA-11426 题意: 求 ∑ gcd(i,j),其中 1<=i<j<=n . 题解:1. 欧拉函数的定义:满足 ...
- UVA 11426 - GCD - Extreme (II) (数论)
UVA 11426 - GCD - Extreme (II) 题目链接 题意:给定N.求∑i<=ni=1∑j<nj=1gcd(i,j)的值. 思路:lrj白书上的例题,设f(n) = gc ...
随机推荐
- 使用AllocConsole()添加调试用控制台
AllocConsole 函数 为调用进程分配一个新的控制台. 使用步骤: 1. AllocConsole(); //分配控制台 2. HANDLE g_hOutput=GetStdHandle( ...
- Bootstrap Multiselect 设置 option
$.ajax({ type: 'post', url: "helper/ajax_search.php", data: {models: decodeURIComponent(br ...
- [转]SpecFlow使用入门
SpecFlow是一个BDD工具,在这里对BDD不多赘述,你可以阅读一下微软2010年十二月的一篇文章,此外如果你想要更多了解SpecFlow,可以参考我的另一篇翻译(当然,也可以直接进入官网) 一. ...
- npm install 不自动生成 package-lock.json文件
package-lock.json这个文件的作用就不详细说明了 有需要的可以参考 :https://www.cnblogs.com/cangqinglang/p/8336754.html 网上都说 n ...
- socket文件权限变更引起crs无法启动故障
Crs无法正常启动,也无法关闭.[root@rac101 ~]# crsctl stop crsStopping resources. This could take several minutes. ...
- MathType编辑钢筋符号就是这么简单
很多的用户在使用MathType公式编辑器的时候,发现它所包含的符号非常的多,几乎你在数学中看到的任何符号都能用MathType编辑出来.它能够满足各个学科对符号的需求,除了常规的数学物理符号之外,也 ...
- Python学习-一个简单的计时器
在实际开发中,往往想要计算一段代码执行多长时间,以下我将该功能写入到一个函数里面,仅仅要在每一个函数前面调用该函数就可以,见以下代码: #------------------------------- ...
- mysql查询某天是本年第几周
SELECT WEEK(date_add('2012-12-31',interval 6 day),2);
- POJ 2609 Ferry Loading(双塔DP)
Ferry Loading Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1807 Accepted: 509 Sp ...
- 解决"django-registration(1048,“column”last_login“不能为null”)
去数据库(MySQL终端): 1.查看django_migrations表的信息.如果你看到一些记录,删除它们(如果migrations目录下有原来的配置,需要清除). mysql> SELEC ...