【POJ】2480 Longge's problem(欧拉函数)
题目
传送门:QWQ
分析
题意就是求∑gcd(i, N) 1<=i <=N.。
显然$ gcd(i,n) = x $时,必然$x|n$。
所以我们枚举一下n的约数,对于每个约数x,显然$ gcd(i/x,n/x)=1$
所以我们计算一下n/x的欧拉函数就ok了。
联赛前刷水题qwq
代码
// #include <bits/stdc++.h>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = ;
ll a[maxn], top=, p[maxn];
int fac(ll x) {
int n=x, flag=;
ll end = sqrt(x);
for(ll i=;i<=end;i++) {
if(x%i==) {
flag=;
a[++top] = i;
if(i*i!=x) a[++top] = x/i;
while(n%i==) n/=i;
}
}
if(x==) flag=;
return flag;
}
ll phi(ll x) {
ll ans = x, end = sqrt(x)+;
for(ll i=;i<=end;i++) {
if(x%i==) {
ans = ans / i * (i-);
while(x%i==) x/=i;
}
}
if(x>) ans = ans / x * (x-);
return ans;
}
void Ph() {
for(int i=;i<maxn-;i++) p[i]=i;
for(ll i=;i<maxn-;i++) {
if(p[i]==i) {
for(ll j=i;j<maxn-;j+=i) {
p[j] = p[j] / i * (i-);
}
}
}
}
int main() {
int n; Ph(); while(scanf("%lld",&n)==) {
if(n==){
printf("1\n"); continue;
}
top=; int q=fac(n);
if(q == ) {
printf("%lld\n",phi(n)+n); continue;
}
sort(a+,a++top);
int pp = top;
ll ans=phi(n)+n;
for(int i=;i<=pp;i++) {
ll qwq;
if(n/a[i] < maxn-) qwq = p[n/a[i]];
else qwq = phi(n/a[i]);
ans += a[i] * qwq;
}
printf("%lld\n",ans);
}
}
【POJ】2480 Longge's problem(欧拉函数)的更多相关文章
- poj 2480 Longge's problem [ 欧拉函数 ]
		
传送门 Longge's problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7327 Accepted: 2 ...
 - POJ 2480 Longge's problem 欧拉函数—————∑gcd(i, N) 1<=i <=N
		
Longge's problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6383 Accepted: 2043 ...
 - poj 2480  Longge's problem 欧拉函数+素数打表
		
Longge's problem Description Longge is good at mathematics and he likes to think about hard mathem ...
 - poj 2480 Longge's problem 积性函数
		
思路:首先给出几个结论: 1.gcd(a,b)是积性函数: 2.积性函数的和仍然是积性函数: 3.phi(a^b)=a^b-a^(b-1); 记 f(n)=∑gcd(i,n),n=p1^e1*p2^e ...
 - 题解报告:poj 2480 Longge's problem(欧拉函数)
		
Description Longge is good at mathematics and he likes to think about hard mathematical problems whi ...
 - POJ 2480 Longge's problem (积性函数,欧拉函数)
		
题意:求∑gcd(i,n),1<=i<=n思路:f(n)=∑gcd(i,n),1<=i<=n可以知道,其实f(n)=sum(p*φ(n/p)),其中p是n的因子.为什么呢?原因 ...
 - poj 3090 && poj 2478(法雷级数,欧拉函数)
		
http://poj.org/problem?id=3090 法雷级数 法雷级数的递推公式非常easy:f[1] = 2; f[i] = f[i-1]+phi[i]. 该题是法雷级数的变形吧,答案是2 ...
 - BZOJ 2705: [SDOI2012]Longge的问题 [欧拉函数]
		
2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 2553 Solved: 1565[Submit][ ...
 - POJ 2478 Farey Sequence(欧拉函数前n项和)
		
A - Farey Sequence Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
 - Bzoj 2705: [SDOI2012]Longge的问题  欧拉函数,数论
		
2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 1959 Solved: 1229[Submit][ ...
 
随机推荐
- Linux:man命令显示颜色
			
man命令显示颜色 在.bashrc下添加 export LESS_TERMCAP_mb=$'\E[01;31m' export LESS_TERMCAP_md=$'\E[01;31m' export ...
 - SWIFT中获取当前经伟度
			
很多的APP中都会用到用户的当前位置信息,本文将实现这个小功能 import UIKit import CoreLocation //添加引用 class ViewController: UIView ...
 - Centos编译Unix网络编程(第三版)卷1的源代码
			
测试环境:Centos 1)在shell中输入./configure然后按回车(注意先让configure有执行权限 chomd 777 configure) 2)依次进入lib.libfree.li ...
 - X11 fluxbox窗口管理器
			
/********************************************************************************* * X11 fluxbox窗口管理 ...
 - Fire Game  双向bfs
			
Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns) ...
 - 在各OJ上的名号
			
POJ MekakuCityActors 牛客 MekakuCityActors hdoj MekakuCityActors 这几个难度较大,所以用Actors 博客 MekakuCityActor ...
 - 《DSP using MATLAB》Problem 3.17
			
用差分方程两边进行z变换,再变量带换得到频率响应函数(或转移函数,即LTI系统脉冲响应的DTFT). 代码: %% ------------------------------------------ ...
 - hdu1165 规律递推
			
题意:给了公式,求A(m,n). 并不知道为什么被杭电分类塞进了dp专题,但是我一开始显然看到这个题就觉得给那个公式用函数递归一下答案肯定能出来了,只不过既然放在了dp专题里面估计这样暴力求解会TLE ...
 - pandas 基础用法
			
pandas 是一个基于 Numpy 构建, 强大的数据分析工具包 主要功能 独特的数据结构 DataFrame, Series 集成时间序列功能 提供丰富的数学运算操作 灵活处理缺失数据 Serie ...
 - && 和 || 运算
			
a() && b() :如果执行a()后返回true,则执行b()并返回b的值:如果执行a()后返回false,则整个表达式返回a()的值,b()不执行: a() || b() :如果 ...