GCD - Extreme (II) (欧拉函数妙用)
https://cn.vjudge.net/problem/UVA-11426
题意:求
解题思路:我们可以定义一个变量dis【n】,dis【n】意为1~(n-1)与n的gcd(最大公约数)的总和,那么可以得到ans【n】=ans【n-1】+dis【n】,那么问题来了,如何求dis【n】呢?我们可以假设一个变量a【i】,a【i】为gcd(n,m)==i (1<=m<n)的个数,那么dis【n】=sum{a【i】*i}了,由gcd(n,m)=i得,gcd(n/i,m/i)=1,即dis【n】=sum{phi【n/i】*i}。
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
#define ri register int
typedef long long ll; inline ll gcd(ll i,ll j){
return j==0?i:gcd(j,i%j);
}
inline ll lcm(ll i,ll j){
return i/gcd(i,j)*j;
}
inline void output(int x){
if(x==0){putchar(48);return;}
int len=0,dg[20];
while(x>0){dg[++len]=x%10;x/=10;}
for(int i=len;i>=1;i--)putchar(dg[i]+48);
}
inline void read(int &x){
char ch=x=0;
int f=1;
while(!isdigit(ch)){
ch=getchar();
if(ch=='-'){
f=-1;
}
}
while(isdigit(ch))
x=x*10+ch-'0',ch=getchar();
x=x*f;
}
const int maxn=4e6+5;
ll ans[maxn];
ll dis[maxn];
int phi[maxn];
int vis[maxn];
void work(){
for(int i=1;i<=4e6+1;i++){
phi[i]=i;
}
for(int i=2;i<=4e6+1;i++){
if(vis[i]==0){
for(int j=i;j<=4e6+1;j+=i){
vis[j]=1;
phi[j]=phi[j]/i*(i-1);
}
}
}
for(int i=1;i<=4e6+1;i++){
for(int j=i*2;j<=4e6+1;j+=i){
dis[j]+=phi[j/i]*i;
}
}
ans[2]=dis[2];
for(int i=3;i<=4e6+1;i++){
ans[i]=ans[i-1]+dis[i];
}
int n;
while(scanf("%d",&n)&&n>0){
printf("%lld\n",ans[n]);
}
return ;
}
int main(){
work();
return 0;
}
GCD - Extreme (II) (欧拉函数妙用)的更多相关文章
- UVA11426 GCD - Extreme (II) (欧拉函数/莫比乌斯反演)
UVA11426 GCD - Extreme (II) 题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 10 100 200000 0 输出样例#1: 67 13 ...
- 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:Here ...
- UVA11426 GCD - Extreme (II)---欧拉函数的运用
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 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) 值为 ...
- UVA11426 GCD - Extreme (II) —— 欧拉函数
题目链接:https://vjudge.net/problem/UVA-11426 题意: 求 ∑ gcd(i,j),其中 1<=i<j<=n . 题解:1. 欧拉函数的定义:满足 ...
- 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) 此 ...
- GCD - Extreme(欧拉函数变形)
题目链接:https://vjudge.net/problem/UVA-11426 题目大意: 给出整数n∈[2,4000000],求解∑gcd(i,j),其中(i,j)满足1≤i<j≤n. 的 ...
随机推荐
- vs2017 打开cs文件提示无法识别的GUID格式
总结一句话 no zuo no die. 是我自己在注册表中给vs增加了自动以管理员身份运行,把值给错了,弄成了 ~ RUNASADMIN WIN7RTM, 改成 ~ RUNASADMIN 后OK.还 ...
- selenium 安装
selenium 安装 一.chromerdriver 1.浏览器版本 1)检查谷歌浏览器版本 打开chrome输入 "chrome://version/"查看版本,如图所示: 2 ...
- selenium 网络请求
selenium 网络请求 browser.find_element_by_id("id的name")browser.find_element("")brows ...
- 第三章 jQuery总结 参考文本
jQuery jQuery是javascript的一个函数库,非常方便,非常主流 利用jQuery开发步骤: 1导入jQuery库 2在$(function(){})的{}中编写jQuery代码 ①j ...
- C++ std::async vs async/await in C# - Stack Overflow
C++ std::async vs async/await in C# - Stack Overflow 我想知道新的c ++功能std::async是否与两个C#关键字async / await相当 ...
- linux上安装memcached步骤
libevent: http://libevent.org/ 服务器端:https://code.google.com/archive/p/memcached/downloads 客户端: http: ...
- Android的发展历史
Android一词最早出现于法国作家利尔亚当(Auguste Villiers de l’Isle-Adam)在1886年发表的科幻小说<未来夏娃>(L’ève future)中.他将外表 ...
- windows7 64位安装tensorflow 1.4.0 CPU版本
机器学习和深度学习真是新生代的宠儿,我也被安排来搞这个了,这下是真的从0开始了.看了几天ppt,想跑跑代码试试,装个环境. 都说tensorflow很火很好用,反正我什么也不懂,准备把这些框架一个一个 ...
- 关于想通过ros的dstnat实现公网IP nat 公网IP的情况
转发群里大神的“苏州-海豚在跳舞”的图 理论上客户机收到2.2.2.2过来的包应该直接丢弃.因为它并没有给2.2.2.2发过包.这里不知道如果在2.2.2.2上做 src-nat伪装成1.1.1.1回 ...
- 干掉hao123劫持浏览器主页
原因可能是安装某个软件流氓捆绑了IE主页导致的,建议这样尝试: 一.如果安装有三方安全防护类软件,排查流氓软件,建议运行系统自带的Windows Defender或者MSE程序扫描系统. 二.如果有检 ...