uva 11426 线性欧拉函数筛选+递推
Problem J GCD Extreme (II)
Input: Standard Input
Output: Standard Output
Given the value of N, you will have to find the value of G. The definition of G is given below:
Here GCD(i,j) means the greatest common divisor of integer i and integer j.
For those who have trouble understanding summation notation, the meaning of G is given in the following code:
|
G=0; for(i=1;i<N;i++) for(j=i+1;j<=N;j++) { G+=gcd(i,j); } /*Here gcd() is a function that finds the greatest common divisor of the two input numbers*/ |
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 corresponding N. The value of G will fit in a 64-bit signed integer.
Sample Input
10
100
200000
0
Output for Sample Input
67
13015
143295493160
/*
题目大意给出一个n,求sum(gcd(i,j),0<i<j<=n);
可以明显的看出来s[n]=s[n-1]+f[n];
f[n]=sum(gcd(i,n),0<i<n);
现在麻烦的是求f[n]
gcd(x,n)的值都是n的约数,则f[n]=
sum{i*g(n,i),i是n的约数},注意到gcd(x,n)=i的
充要条件是gcd(x/i,n/i)=1,因此满足条件的
x/i有phi(n/i)个,说明gcd(n,i)=phi(n/i).
f[n]=sum{i*phi(n/i),1=<i<n}
因此可以搞个for循环对i循环,只要i<n,f[n]+=i*phi(n/i);
*/
#include<iostream>
#include<cstdio>
using namespace std;
#define Max 4000000 __int64 s[Max+],f[Max+],phi[Max+];
int prime[Max/];
bool flag[Max+]; void Init()
{
int i,j,num=;
memset(flag,,sizeof(flag));
phi[]=;
for(i=;i<=Max;i++)//欧拉筛选
{
if(flag[i])
{
prime[num++]=i;
phi[i]=i-;
}
for(j=;j<num && prime[j]*i<=Max;j++)
{
flag[i*prime[j]]=false;
if(i%prime[j]==)
{
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
else phi[i*prime[j]]=phi[i]*(prime[j]-);
}
}
memset(f,,sizeof(f));
for(i=;i<=Max;i++)//f[n]更新
{
//因为f[n]=gcd[1,n]+....+gcd[n-1,n]所以j=2*i开始,若从j=i开始那就等同于加上了一项gcd(n,n)
for(j=*i;j<=Max;j+=i)
f[j]+=i*phi[j/i];
}
memset(s,,sizeof(s));
for(i=;i<=Max;i++)
s[i]=s[i-]+f[i];
} int main()
{
Init();
int n;
while(cin>>n,n)
printf("%I64d\n",s[n]);
return ;
}
uva 11426 线性欧拉函数筛选+递推的更多相关文章
- GCD - Extreme (II) UVA - 11426(欧拉函数!!)
G(i) = (gcd(1, i) + gcd(2, i) + gcd(3, i) + .....+ gcd(i-1, i)) ret = G(1) + G(2) + G(3) +.....+ G(n ...
- POJ_2478 Farey Sequence 【欧拉函数+简单递推】
一.题目 The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbe ...
- UVA 12493 Stars (欧拉函数--求1~n与n互质的个数)
pid=26358">https://uva.onlinejudge.org/index.phpoption=com_onlinejudge&Itemid=8&cate ...
- 紫书 例题 10-7 UVa 10820 (欧拉函数)
这道题要找二元组(x, y) 满足1 <= x, y <= n 且x与y互素 那么我就可以假设x < y, 设这时答案为f(n) 那么答案就为2 * f(n) +1(x与y反过来就乘 ...
- 紫书 例题 10-27 UVa 10214(欧拉函数)
只看一个象限简化问题,最后答案乘4+4 象限里面枚举x, 在当前这条固定的平行于y轴的直线中 分成长度为x的一段段.符合题目要求的点gcd(x,y) = 1 那么第一段1<= y <= x ...
- bzoj 2818 gcd 线性欧拉函数
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MB[Submit][Status][Discuss] Description 给定整数N,求1< ...
- bzoj 2190: [SDOI2008]仪仗队 线性欧拉函数
2190: [SDOI2008]仪仗队 Time Limit: 10 Sec Memory Limit: 259 MB[Submit][Status][Discuss] Description 作为 ...
- 紫书 例题 10-26 UVa 11440(欧拉函数+数论)
这里用到了一些数论知识 首先素因子都大于M等价与M! 互质 然后又因为当k与M!互质且k>M!时当且仅当k mod M! 与M!互质(欧几里得算法的原理) 又因为N>=M, 所以N!为M! ...
- POJ2478(SummerTrainingDay04-E 欧拉函数)
Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16927 Accepted: 6764 D ...
随机推荐
- php接口开发注意事项
IOS Object c 强类型 Android java 强类型 wap javascript 弱类型 后台 php 弱类型 开发接口 wap和app共用 强类型语言可能要求返回的值是数组就要保 ...
- 通过StringBuilder的reverse()实现倒序
import java.util.Scanner; public class ReverseString { public static void main(String[] args) { Scan ...
- PE基础2
PE课程002 怎么找到Nt头? (PIMAGE_NT_HEADER)(DOS.e_lfanew + (DWORD)m_pBuff) 怎么找到第一个区段表? 区段头位置 = pNt + 4 + 文件头的 ...
- 当然,perl等脚本服务器是一般默认安装了,你入侵了一台主机,总不能先装配 Java 环境然后再开干吧?
转自:https://www.zhihu.com/question/20173592 当然,perl等脚本服务器是一般默认安装了,你入侵了一台主机,总不能先装配 Java 环境然后再开干吧?
- ios 团购信息客户端demo(三)
接上二篇的内容,今天我们就来介绍一下如何将解析出来的数据放入AQGridView中显示出来,因为我们的工程中已经将AQGridView导入了,所以我们在KKFirstViewController中直接 ...
- javascript基础知识 (八) BOM学习笔记
一.什么是BOM BOM(Browser Object Model)即浏览器对象模型. BOM提供了独立于内容 而与浏览器窗口进行交互的对象: 由于BOM主要用于管理窗口 ...
- vue父子传值的具体应用
最近我负责的项目已经迭代到第四版了,我作为一个没啥经验的小菜鸟也成长了很多. 在这一版开发开始之前,我老大就要求我在开发过程中尽量实现组件化,因此,我也遇到了很多问题,但基本都解决了,所以趁周末把这些 ...
- PyCharm(一)——PyCharm设置SSH远程调试
一.环境 系统环境:windows10 64位 软件:PyCharm2017.3 本地Python环境:Python2.7 二.配置 2.1配置远程调试 第一步:运行PyCharm,然后点击设置如下图 ...
- (原)剑指offer跳台阶和矩形覆盖
跳台阶 时间限制:1秒空间限制:32768K 题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 分析同样为斐波那契数列边形这样的题肯定有公式 设 ...
- python模块--random
random主要用于生成随机字符串等,例如登录页面上随机字符串验证. random常用方法: import random print(random.randrange(1, 10)) # 返回1-10 ...