题目

传送门: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(欧拉函数)的更多相关文章

  1. poj 2480 Longge's problem [ 欧拉函数 ]

    传送门 Longge's problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7327   Accepted: 2 ...

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

  3. poj 2480 Longge's problem 欧拉函数+素数打表

    Longge's problem   Description Longge is good at mathematics and he likes to think about hard mathem ...

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

  5. 题解报告:poj 2480 Longge's problem(欧拉函数)

    Description Longge is good at mathematics and he likes to think about hard mathematical problems whi ...

  6. 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的因子.为什么呢?原因 ...

  7. poj 3090 &amp;&amp; poj 2478(法雷级数,欧拉函数)

    http://poj.org/problem?id=3090 法雷级数 法雷级数的递推公式非常easy:f[1] = 2; f[i] = f[i-1]+phi[i]. 该题是法雷级数的变形吧,答案是2 ...

  8. BZOJ 2705: [SDOI2012]Longge的问题 [欧拉函数]

    2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 2553  Solved: 1565[Submit][ ...

  9. POJ 2478 Farey Sequence(欧拉函数前n项和)

    A - Farey Sequence Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  10. Bzoj 2705: [SDOI2012]Longge的问题 欧拉函数,数论

    2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 1959  Solved: 1229[Submit][ ...

随机推荐

  1. 安装pyenv virtualenv

    地址:https://github.com/pyenv/pyenv-virtualenv Check out pyenv-virtualenv into plugin directory $ git ...

  2. react 学习日记

    1.本地配置代理服务:   create-react-app 创建的react项目 package.jsoin 中 加入: "proxy": "http://localh ...

  3. ListBox 控件单击事件

    XAML: <ListBox x:Name="ItemBox" Grid.Column="0" Tap="ItemBox_Tap"&g ...

  4. JS禁止用F5键

    //禁止用F5键 function document.onkeydown() { if ( event.keyCode==116) { event.keyCode = 0; event.cancelB ...

  5. ubunttu-sh: 1: pause: not found

    old code: //in ubuntu OS system("pause"); error discription: : pause: not found right code ...

  6. ITelephony.aidl

    在src下先建立包名为com.android.internal.telephony(右键src > new > package,create package-info.java打钩),然后 ...

  7. 关于bfs时间轴

    对于bfs,由于是通过不断将平行位置的元素加入到队列进行的,所以它在一定情况下淡化了与队列外部的  "时间"   联系观念,通过一个数组记录内部的 "时间" 这 ...

  8. tp5.1 错误 No input file specified.

    http://www.xxxx.com/admin/index/index 出现错误:No input file specified. 一.方法 与php版本有关  PHP版本5.6以上都会出现这个问 ...

  9. Java JDK 版本的区别

    jdk6和jdk5相比的新特性有: 1.instrumentation 在 Java SE 6 里面,instrumentation 包被赋予了更强大的功能:启动后的 instrument.本地代码 ...

  10. index.do