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. 的 ...
随机推荐
- python基础-闭包
def bibao(): li = [] n = [1] def inner(): li.append(n[0]) n[0] +=1 print(li) return inner b = bibao( ...
- Docker的网络类型和固定IP设置
Docker的网络机制 Docker的网络有三种类型(driver): bridge, host 和 null. birdge: 就如同桥接的switch/hub, 使用bridge网络的contai ...
- MVC Ajax.BeginForm 提交上传图片
吃水不忘挖井人,如果对你有帮助,请说声谢谢.如果你要转载,请注明出处.谢谢! 异步提交时,出现图片不能上传. 起初我定格在 System.Web.Mvc 中.查询源码时,也是没有问题的.那问题出现 ...
- 将Long类型转为字母数字组合的jar包---Hashids
在设计数据库时,我有时喜欢使用自增Id,而不是uuid,但是在面对终端用户时,直接暴露id不是一个好的行为. 经过查询,可以使用 Hashids 这个jar包将id转为类似YouTube的大小写字母和 ...
- C#编程经验-function advanced params
function advanced params:outrefparamsnot useful,to use them is a burden,so i dont use themdefects:ou ...
- 【fork/join】java并发编程-fork/join示例
package com.chinamobile.epic.tako.common.graphite.query.sync.impl; import com.google.common.collect. ...
- 初识TypeScript
环境配置 1,全局安装node和npm 2,安装TypeScript包 npm install typescript -g tsc --version 编写第一个ts程序 1,初始化项目 新建文件夹d ...
- 学习笔记之Problem Solving with Algorithms and Data Structures using Python
Problem Solving with Algorithms and Data Structures using Python — Problem Solving with Algorithms a ...
- MYSQL ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.10.210' (111) 解决方法
今天在测试MySQL的连接时候,发现连接不通过,并报错ERROR 2003 (HY000): Can't connect to mysql server on '192.168.10.210' (11 ...
- 1-hadoop、mr
1.HDFS的优缺点: 优点: ① 高容错 ② 可扩展 ③ 适合大文件存储 ④ 可构建在廉价的机器上 缺点: ① 高延迟 ② 文件不能修改 ③ 不适合小文件存储 2.HDFS架构(类似于文件系统): ...