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 ...
随机推荐
- Tuning 05 Sizing other SGA Structure
Redo Log Buffer Content The Oracle server processes copy redo entries from the user’s memory space t ...
- vs2003 不断提示 已过期 问题
工作时曾遇到使用 vs2003 生成后,点击"调试“或者"执行“后不断提示 类似“已过期,是否要重新生成”这样的问题. 当时的情况是 :我要实现的功能和"时间" ...
- Jmeter实现对字符串加密
最近测试移动端接口,但是请求内容是用MD5加密的,所以要先对请求内容进行加密,Jmeter内置的没有MD5加密方法,所以自己从网上copy了一份,实现了加密功能,以下是具体操作: 1.从网上copy了 ...
- (转载)【C#4.0】dynamic和var及object
dynamic a = 10;a = a + 10;Console.WriteLine(a.GetType()); 此段代码会输出 System.Int32,第二行不需要类型转换,因为在运行时识别类型 ...
- backbone Model调用save方法的时候提交方式
horizon使用的是backbone框架,但是我们的后台api都是只接收post请求,请求的路径为/api/,根据backbone的官档解释: backbone的model.save方法会判断当前的 ...
- jQuery 事件的命名空间的含义
对于jquery的on的events解释是 一个或多个空格分隔的事件类型和可选的命名空间,或仅仅是命名空间,比如"click", "keydown.myPlugin&qu ...
- 五月的仓颉大神写的 三年java程序员面试感悟 值得分享给大家
感谢 五月的仓颉 的这篇文章 , 让我重新认识到自己身上的不足之处 . 原文地址http://www.cnblogs.com/xrq730/p/5260294.html,转载请注明出处,谢谢! 前 ...
- python 保存csv文件
利用pandas库, 将numpy的array数据保存成csv格式的文件: import pandas as pd import numpy as np data = pd.read_csv('C:\ ...
- No transactional EntityManager available; nested exception is javax.persistence.TransactionRequiredException: No transactional EntityManager available
参考地址:http://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/ ...
- 160805、oracle查询:取出每组中的第一条记录
在Java 9发布之前,我们来分享一些Java 8开发技巧 [以下为译文] 在使用JAVA 8进行开发多年后,结合个人使用IntelliJ IDEA的心得,我总结了以下几个JAVA8技巧供大家参考. ...