Longge的问题

Description

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

Input

一个整数,为N。

Output

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

Sample Input

6

Sample Output

15

Hint

【数据范围】
对于60%的数据,0<N<=2^16。
对于100%的数据,0<N<=2^32。

题解:题目让求∑gcd(i, N)(1<=i <=N);很显然暴力是不行的,由于gcd(m,n)=k,所以gcd(m/k,n/k)=1;要求小于等于n的最大公约数是k的个数num[k]等于ouler[n/k];枚举k求k*num[k]的和即可;

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<vector>
#include<map>
#include<string>
using namespace std;
typedef long long LL;
LL ouler(LL n){
LL ans=n;
for(LL i=;i*i<=n;i++){
if(n%i==){
ans=ans*(i-)/i;
while(n%i==)n/=i;
}
}
if(n>)ans=ans*(n-)/n;
return ans;
}
int main(){
LL N;
while(~scanf("%lld",&N)){
LL ans=;
for(LL i=;i*i<=N;i++){
if(N%i==){
ans+=ouler(N/i)*i;
if(N/i>sqrt(1.0*N))ans+=ouler(i)*(N/i);//这里ouler[N/N/i]=ouler[i]计算最大公约数是N/i的个数
}
}
printf("%lld\n",ans);
}
return ;
}

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: 1959  Solved: 1229[Submit][ ...

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

  4. 【bzoj2705】[SDOI2012]Longge的问题 欧拉函数

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

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

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

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

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

  7. [SDOI2012] Longge的问题 - 欧拉函数

    求 \(\sum\limits_{i=1}^{n}gcd(i,n)\) Solution 化简为 \(\sum\limits_{i|n}^{n}φ(\dfrac{n}{i})i\) 筛出欧拉函数暴力求 ...

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

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

  9. Bzoj-2705 Longge的问题 欧拉函数

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2705 题意: 求 sigma(gcd(i,n), 1<=i<=n<2^3 ...

随机推荐

  1. UESTC_韩爷的梦 2015 UESTC Training for Search Algorithm & String<Problem N>

    N - 韩爷的梦 Time Limit: 200/100MS (Java/Others)     Memory Limit: 1300/1300KB (Java/Others) Submit Stat ...

  2. UESTC_秋实大哥与快餐店 2015 UESTC Training for Data Structures<Problem C>

    C - 秋实大哥与快餐店 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Sub ...

  3. convertView

    [convertView] 参考:https://zhidao.baidu.com/question/423895201122905772.html

  4. PHP MySQL Order By 关键词 之 Order By

    ORDER BY 关键词 ORDER BY 关键词用于对记录集中的数据进行排序. 语法 SELECT column_name(s) FROM table_name ORDER BY column_na ...

  5. java连接oracle数据库详细代码

    详细代码: import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;imp ...

  6. textField 判断输入长度限制

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementS ...

  7. UVa 836 - Largest Submatrix

    题目:给你一个n*n的01矩阵,求里面最大的1组成的矩形的米娜及. 分析:dp.单调队列.UVa 1330同题,仅仅是输入格式变了. 我们将问题分解成最大矩形.即求解以k行为底边的图形中的最大矩形.然 ...

  8. AC自动机跟随Kuangbing学习笔记

    http://www.cnblogs.com/kuangbin/p/3164106.html kuangbin的博客 第一段代码基本是COPY kuangbin的.. 1.HDU 2222 Keywo ...

  9. .net如何后台批量删除

    button_Click(Sender sender,Event e){foreach (DataListItem item in DataList1.Items){CheckBox cbox=(Ch ...

  10. cover letter issues

    All cover letters should: Explain why you are sending a resume. Don't send a resume without a cover ...