http://www.lydsy.com/JudgeOnline/problem.php?id=2705

Description

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

Input

一个整数,为N。

Output

一个整数,为所求的答案。

Sample Input

6

Sample Output

15

————————————————————————————————————————————

http://hzwer.com/3470.html的博客已经讲的蛮清楚了,这里在复述一遍。

gcd(i,n)的值显然是n的约数,这里取k=gcd(i,n),满足该关系式的i的个数为s(k)。

则答案为∑k*s(k)(k|n)

又因为k=gcd(i,n)推出gcd(n/k,i/k)=1,设t=i/k,则n/k与t互质,求出t的个数。

这显然可以用欧拉函数解决,那么s(k)=phi(n/k)

#include<cstdio>
#include<queue>
#include<cctype>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
ll phi(ll x){
ll res=x;
for(ll i=;i*i<=x;i++){
if(x%i==){
res-=res/i;
while(x%i==)x/=i;
}
}
if(x>)res-=res/x;
return res;
}
int main(){
ll n;
scanf("%lld",&n);
ll ans=;
for(int i=;i*i<=n;i++){
if(n%i==){
ans+=(ll)i*phi(n/i);
if(i*i<n)ans+=(ll)(n/i)*phi(i);
}
}
printf("%lld\n",ans);
return ;
}

BZOJ2705:[SDOI2012]Longge的问题——题解的更多相关文章

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

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

  2. 【欧拉函数】BZOJ2705: [SDOI2012]Longge的问题

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

  3. BZOJ2705: [SDOI2012]Longge的问题

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

  4. bzoj2705: [SDOI2012]Longge的问题 欧拉定理

    题意:给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N). 题解:考虑n的所有因子,假设有因子k,那么对答案的贡献gcd(i,n)==k的个数即gcd(i/k,n/k)== ...

  5. BZOJ2705: [SDOI2012]Longge的问题(欧拉函数)

    题意 题目链接 Sol 开始用反演推发现不会求\(\mu(k)\)慌的一批 退了两步发现只要求个欧拉函数就行了 \(ans = \sum_{d | n} d \phi(\frac{n}{d})\) 理 ...

  6. 【数论】【枚举约数】【欧拉函数】bzoj2705 [SDOI2012]Longge的问题

    ∵∑gcd(i, N)(1<=i <=N) =k1*s(f1)+k2*s(k2)+...+km*s(km) {ki是N的约数,s(ki)是满足gcd(x,N)=ki(1<=x< ...

  7. [BZOJ2705][SDOI2012]Longge的问题 数学

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2705 首先分析得题目所求$gcd(i,N)$的取值只可能是$N$的因子,则有$$Ans=\ ...

  8. bzoj2705 [SDOI2012]Longge的问题——因数

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2705 一开始自己想了半天... 有了点思路:遍历 n 的因数 k,每个因数要预处理出 gcd ...

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

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

随机推荐

  1. Hadoop3.0新特性

    1. Hadoop3.0简介 Hadoop 2.0是基于JDK 1.7开发的,而JDK 1.7在2015年4月已停止更新,这直接迫使Hadoop社区基于JDK1.8重新发布一个新的Hadoop版本,而 ...

  2. redis 问题记录

    摘抄来自:https://zhuoroger.github.io/ 1.slowlog和排队延时 slowlog是排查性能问题关键监控指标.它是记录Redis queries运行时间超时特定阀值的系统 ...

  3. python 函数定义顺序

    #!/usr/bin/python # Hello World def order(): print("haha") print('Hello World!') order()

  4. 使用union

    QUERY: explain extended ) TRACE: { "steps": [ { "join_preparation": { "sele ...

  5. Entity Framework Core 选择数据表的外键

    entityTypeBuilder .HasOne<GeraeteArt>() .WithMany(p => p.Geraete) .HasForeignKey(b => b. ...

  6. cf#514B. Forgery(暴力)

    B. Forgerytime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputsta ...

  7. Selenium(Python) ddt读取CSV文件数据驱动

    import csvimport unittestfrom time import sleep from ddt import ddt, data, unpackfrom selenium impor ...

  8. PyCharm添加Selenium与Appium类库

    PyCharm添加Selenium与Appium依赖, 不需要用pip去安装!

  9. leetcode-前K个高频元素

    给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2: 输入: nums = [1], ...

  10. JavaScript 之 对象/JSON/数组

    对象 简单说,所谓对象,就是一种无序的数据集合,由若干个“键值对”(key-value)构成. var obj = { p: 'Hello World' }; 上面代码中,大括号就定义了一个对象,它被 ...