题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=473&problem=2421&mosmsg=Submission+received+with+ID+13800900

Given the value of N, you will have to find the value of G. The definition of G is given below:

Input
The input file contains at most 100 lines of inputs. Each line contains an integer N (1 < N < 4000001).
The meaning of N is given in the problem statement. Input is terminated by a line containing a single
zero.

Output

For each line of input produce one line of output. This line contains the value of G for the correspondingN. The value of G will fit in a 64-bit signed integer.
  
Sample Input
10
100
200000
0
Sample Output
67
13015
143295493160

我们假设b[n]表示1到n-1与n的gcd的和,那么G[n]=G[n-1]+b[n];

a[i]表示与gcd(n, x)= i 的x的个数;b[n]=sum( a[i] * i ) , 所以我们只需求a[i]即可;根据gcd(n, x)=i ----->gcd(n/i, x/i) = 1,

因此仅仅要求出欧拉函数phi(n / i),就能够得到与n / i互质的个数,从而求出gcd(x , n) = i的个数,这样总体就能够求解了

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std; #define N 4000001
typedef long long LL; LL a[N], b[N], dp[N]; int main()
{
for(int i=; i<N; i++)///欧拉打表;
{
if(!a[i])
{
for(int j=i; j<N; j+=i)
{
if(!a[j]) a[j]=j;
a[j]=a[j]/i*(i-);
}
}
} for(int i=; i<N; i++)///[1,n-1]中所有的数与n的gcd的和
for(int j=i*; j<N; j+=i)
b[j] += a[j/i]*i; for(int i=; i<N; i++)
dp[i]=dp[i-]+b[i]; int n;
while(scanf("%d", &n), n)
{
printf("%lld\n", dp[n]);
}
return ;
}

UVA11426 GCD - Extreme (II)---欧拉函数的运用的更多相关文章

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

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

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

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

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

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

  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:Here ...

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

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

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

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

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

  9. GCD - Extreme(欧拉函数变形)

    题目链接:https://vjudge.net/problem/UVA-11426 题目大意: 给出整数n∈[2,4000000],求解∑gcd(i,j),其中(i,j)满足1≤i<j≤n. 的 ...

随机推荐

  1. 纯css3实现的win8加载动画

    今天给大家分享一款纯css3实现的win8加载动画.在这款实例中动画效果完全由css3实现.一起看下效果图: 在线预览   源码下载 实现的代码. html代码: <div class=&quo ...

  2. 一款基于jQuery带事件记录的日历插件

    之前我们也已经分享过不少jQuery日历插件,有些应用了CSS3的特性,外观就特别漂亮.今天要分享的这款jQuery日历插件不仅有着绚丽的外观,而且带有日期事件记录功能,点击日期即可展开事件记录窗口, ...

  3. Okra框架(二) 搭建Socket服务器

    本文将介绍使用Okra框架帮助开发者快速搭建高性能应用程序Socket服务端. 博主接触的网络游戏(包含但不限于网页, 手机)的服务端通信使用的协议基本上就Socket,Http或是WebSocket ...

  4. jquery可拖动表格调整列格子的宽度大小(转)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. php -- php获取ip地址和主机名

    客户端IP相关的变量1. $_SERVER['REMOTE_ADDR']; 客户端IP,有可能是用户的IP,也有可能是代理的IP. 2. $_SERVER['HTTP_CLIENT_IP']; 代理端 ...

  6. 【noip模拟题】日历游戏(博弈论+搜索)

    直接搜索即可... 注意不要爆栈..所以我们可以分块搜索... 然后太懒且太弱我就不写了... orz hzwer http://hzwer.com/4954.html [问题描述] moreD和mo ...

  7. 【BZOJ】1664: [Usaco2006 Open]County Fair Events 参加节日庆祝(线段树+dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1664 和之前的那题一样啊.. 只不过权值变为了1.. 同样用线段树维护区间,然后在区间范围内dp. ...

  8. gcc编译程序的流程

    >>gcc编译器 gcc编译器:(C语言的编译器gcc/g++) gcc编译程序的流程 源文件(.c)——>预处理(.i)——>编译——>汇编(.s)——>链接(. ...

  9. HBaseclientAPI基本操作

    Java类与HBase数据模型 HBaseConfiguration 包名 : org.apache.hadoop.hbase.HBaseConfiguration 作用:对HBase进行配置. 使用 ...

  10. Windows远程访问OEM乱码解决

    问题描述 发现用Windows访问Linux安装的Oracle时oem按钮总是乱码,整理解决方法如下: OEM简介及按钮乱码问题  http://www.linuxidc.com/Linux/2013 ...