求\(\sum_{i=1}^ngcd(i,n)\)

考虑枚举\(gcd\),现在答案变成这样

\(\sum_{d|n}d*f(d)\)

\(f(d)=\sum_{i=1}^n [gcd(i,n)==d]\)

考虑一下\(f(d)\)如何求

显然\(f(d)=\varphi(n/d)\)

因为所有与\(n/d\)互质的数乘上\(d\)就是和\(n\)互质的数了

所有答案就是\(\sum_{d|n}d*\varphi(n/d)\)

代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define re register
#define LL long long
#define maxn 65580
inline int read()
{
char c=getchar();
int x=0;
while(c<'0'||c>'9') c=getchar();
while(c>='0'&&c<='9')
x=(x<<3)+(x<<1)+c-48,c=getchar();
return x;
}
int phi[maxn],p[maxn],f[maxn];
LL n,ans;
int U;
inline LL solve(LL x)
{
if(x<=U) return phi[x];
LL now=1;
for(re int i=1;i<=p[0];i++)
if(x%p[i]==0)
{
LL tot=1;
while(x%p[i]==0) tot*=p[i],x/=p[i];
now*=(p[i]-1)*(tot/p[i]);
if(x==1) break;
}
if(x!=1) now*=(x-1);
return now;
}
int main()
{
scanf("%lld",&n);
f[1]=1,phi[1]=1;
U=std::sqrt(n);
for(re int i=2;i<=U;i++)
{
if(!f[i]) p[++p[0]]=i,phi[i]=i-1;
for(re int j=1;j<=p[0]&&p[j]*i<=U;j++)
{
f[p[j]*i]=1;
if(i%p[j]==0)
{
phi[p[j]*i]=phi[i]*p[j];
break;
}
phi[p[j]*i]=phi[i]*(p[j]-1);
}
}
for(re LL i=1;i*i<=n;i++)
if(n%i==0)
{
ans+=i*solve(n/i);
if(n/i!=i) ans+=(n/i)*solve(i);
}
std::cout<<ans;
return 0;
}

【[SDOi2012]Longge的问题】的更多相关文章

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

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

  2. BZOJ 2705: [SDOI2012]Longge的问题

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

  3. BZOJ 2705: [SDOI2012]Longge的问题 GCD

    2705: [SDOI2012]Longge的问题 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnl ...

  4. bzoj 2705: [SDOI2012]Longge的问题 歐拉函數

    2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 1035  Solved: 669[Submit][S ...

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

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

  6. BZOJ 2705: [SDOI2012]Longge的问题( 数论 )

    T了一版....是因为我找质因数的姿势不对... 考虑n的每个因数对答案的贡献. 答案就是 ∑ d * phi(n / d) (d | n) 直接枚举n的因数然后求phi就行了. 但是我们可以做的更好 ...

  7. 洛谷 P2303 [SDOi2012]Longge的问题 解题报告

    P2303 [SDOi2012]Longge的问题 题目背景 SDOi2012 题目描述 Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数\(N\),你需要 ...

  8. BZOJ2705 SDOI2012 Longge的问题 【欧拉函数】

    BZOJ2705 SDOI2012 Longge的问题 Description Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数N,你需要求出∑gcd(i, ...

  9. 【bzoj2705】[SDOI2012]Longge的问题

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

  10. [SDOi2012]Longge的问题 (数论)

    Luogu2303 [SDOi2012]Longge的问题 题目 题目背景 SDOi2012 题目描述 Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数N, ...

随机推荐

  1. zabbix 监控ipmi

    一,配置ipmi yum -y install OpenIPMI OpenIPMI-devel ipmitool freeipmi 登入IDARAC 在服务端测试是否可以获得数据 ipmitool - ...

  2. ButtonAddListener监听按钮点击事件

    ButtonAddListener监听按钮点击事件 using UnityEngine; using System.Collections; using UnityEngine.UI; using U ...

  3. linux中Python源码安装和配置

    安装 首先获取安装包,此处版本为3.7 wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz 解压 tar xvf Python- ...

  4. Python 断言的使用方法

    自动化测试常用断言的使用方法(python) 自动化测试中寻找元素并进行操作,如果在元素好找的情况下,相信大家都可以较熟练地编写用例脚本了,但光进行操作可能还不够,有时候也需要对预期结果进行判断. 这 ...

  5. [Activator-HelloAkka] Create our Actors

    So far we have defined our Actor and its messages. Now let's create an instance of this actor. In Ak ...

  6. Java入门系列-07-从控制台中接收输入

    这篇文章帮你使用Scanner类从控制台接收输入 从控制台接收字符串 敲一敲: import java.util.Scanner; public class DemoScanner { public ...

  7. java I/O流 温习随笔

    java I/O流的熟练掌握是十分重要的. 在我的理解中,I/O流可以分为两种:字符流.字节流.字符流就是可以用来传输字符的流,比如传输txt文本,简单的说,只有能被电脑中的记事本直接打开并且你能看懂 ...

  8. 安装 VS 2015 Update 2 + Windows SDK Tools 1.3.1 + Windows SDK 10586.212 后提示找不到 10586.0 SDK 问题的解决方法

    将 Visual Studio 2015 升级到 Update 2,并安装 Windows SDK Tools 1.3.1 和 Windows SDK 10586.212 后,有可能造成原本已安装的 ...

  9. asp.net之cookie

    1.创建cookie HttpCookie userCookie = new HttpCookie("userInfo"); userCookie["name" ...

  10. MVC3.0与MVC2.0的区别

    昨天面试时第一回用MVC2.0做了一个简单的增删改查功能的测试.想一下用了一年多的MVC3.0,对这两个版本不同之处做以下几点总结: 最明显的是MVC3.0较MVC2.0而言,多了Razor视图: 1 ...