思路:

虽然看到题目就想到了用欧拉函数做,但就是不知道怎么做...

当a b互质时GCD(a,b)= 1,由此我们可以推出GCD(k*a,k*b)= k。设ans[i]是1~i-1与i的GCD之和,所以最终答案是将ans[0]一直加到ans[n]。当 k*i==j 时,ans[j]=k*euler[i]。

看完题解瞬间领悟:神奇海螺

突然忘记欧拉函数是什么:欧拉函数

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
#define ll long long
const int N=4000005;
const int M=4000000;
const int MOD=1000;
using namespace std;
ll ans[N];
ll euler[N];
void get(){
memset(ans,0,sizeof(ans));
for(int i=1;i<=M;i++){
euler[i]=i;
}
for(int i=2;i<=M;i++){
if(euler[i]==i){ //i是质数
for(int j=i;j<=M;j+=i){
euler[j]=euler[j]/i*(i-1);
}
}    //得到与i互质的个数
for(int k=1;k*i<=M;k++){    
ans[k*i]+=k*euler[i];
}
}
for(int i=2;i<=M;i++){
ans[i]+=ans[i-1];
}
}
int main(){
get();
int T,num=1,n;
while(~scanf("%d",&n) && n){
printf("%lld\n",ans[n]);
}
return 0;
}

UVA 11426 GCD - Extreme (II) (欧拉函数)题解的更多相关文章

  1. UVA 11426 GCD - Extreme (II) (欧拉函数+筛法)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70017#problem/O 题意是给你n,求所有gcd(i , j)的和,其中 ...

  2. 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 ...

  3. 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) 值为 ...

  4. 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< ...

  5. UVA 11426 GCD - Extreme (II) 欧拉函数

    分析:枚举每个数的贡献,欧拉函数筛法 #include <cstdio> #include <iostream> #include <ctime> #include ...

  6. 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) 此 ...

  7. UVA11426 GCD - Extreme (II) (欧拉函数/莫比乌斯反演)

    UVA11426 GCD - Extreme (II) 题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 10 100 200000 0 输出样例#1: 67 13 ...

  8. UVA11426 GCD - Extreme (II)---欧拉函数的运用

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. UVA11426 GCD - Extreme (II) —— 欧拉函数

    题目链接:https://vjudge.net/problem/UVA-11426 题意: 求 ∑ gcd(i,j),其中 1<=i<j<=n . 题解:1. 欧拉函数的定义:满足 ...

  10. UVA 11426 - GCD - Extreme (II) (数论)

    UVA 11426 - GCD - Extreme (II) 题目链接 题意:给定N.求∑i<=ni=1∑j<nj=1gcd(i,j)的值. 思路:lrj白书上的例题,设f(n) = gc ...

随机推荐

  1. 迷宫问题---poj3984(bfs,输出路径问题)

    题目链接 主要就是输出路径问题: pre[x][y]表示到达(x,y)是由点(pre[x][y].x,  pre[x][y].y)而来: #include<stdio.h> #includ ...

  2. paas容器云

  3. 建立一个更高级别的查询 API:正确使用Django ORM 的方式(转)

    add by zhj: 本文作者是DabApps公司的技术主管,作者认为在view中直接使用Django提供的ORM查询方法是不好的,我对此并不赞同,可能作者 写这篇文章是给Django的初学者看,所 ...

  4. (3.11)mysql基础深入——mysql文件分类与配置文件管理

    (3.11)mysql基础深入——mysql文件分类与管理 关键词:mysql配置文件,mysql参数文件,mysql中的my.cnf 目录:mysql数据库文件分类: [1]参数文件:my.cnf ...

  5. Shell初学(三)传参

    一. 脚本代码:test.sh echo "Shell 传递参数实例!"; echo "执行的文件名:$0"; echo "第一个参数为:$1&quo ...

  6. iframs刷新的两种方法

    <iframe src="a1.html" id="iframe1Id" name="iframe1Name" width=" ...

  7. Goroutines vs Threads

    http://tleyden.github.io/blog/2014/10/30/goroutines-vs-threads/ Here are some of the advantages of G ...

  8. tomcat访问

    1:html页面或者需要访问的对象需要放置到webapps/ROOT下面既可以  http://localhost:8080/直接访问 2:

  9. PAT 1017 Queueing at Bank[一般]

    1017 Queueing at Bank (25)(25 分)提问 Suppose a bank has K windows open for service. There is a yellow ...

  10. windows server r2 之如何设置共享文件夹访问不需要输入用户名和密码

    第一步: 打开guest账号.单击桌面“开始”按钮,找到“控制面板”并打开,选择“用户帐户”并单击就会弹出一个窗口,继续单击下方的“管理其他帐户”,然后选择“Guest”,点击“启用”. 第二步: 在 ...