2705: [SDOI2012]Longge的问题

Time Limit: 3 Sec  Memory Limit: 128 MB
Submit: 1035  Solved: 669
[Submit][Status]

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。

Source

  枚舉n的因數k,算出gcd(i,n)==k,即gcd(i/k,n/k)==1,的數個數,即phi(n/k)。

  這道題讓我意識到我思考問題的角度還是不夠廣,總是憑着第一感覺想下去,完全無法做到思路的變通。
  以這道題爲例,我一看題,就認定這是莫比烏斯反演的題目,根本沒有思考其他的方式。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<string>
#include<queue>
using namespace std;
#ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif
#define MAXN 1100000
#define MAXV MAXN*2
#define MAXE MAXV*2
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
typedef long long qword;
inline int nextInt()
{
char ch;
int x=;
bool flag=false;
do
ch=getchar(),flag=(ch=='-')?true:flag;
while(ch<''||ch>'');
do x=x*+ch-'';
while (ch=getchar(),ch<='' && ch>='');
return x*(flag?-:);
} qword n,m;
qword prime[MAXN],topp=-;
bool pflag[MAXN];
int gcd(int x,int y)
{
return (x%y==) ? y : gcd(y,x%y);
}
void init()
{
int i,j;
for (i=;i<MAXN;i++)
{
if (!pflag[i])
{
prime[++topp]=i;
}
for (j=;j<=topp && i*prime[j]<MAXN;j++)
{
pflag[i*prime[j]]=true;
if (i%prime[j]==)
{
break;
}
}
}
}
qword phi(qword x)
{
int i;
qword ret=;
for (i=;i<=topp && prime[i]*prime[i]<=x;i++)
{
if(x%prime[i]==)
{
ret*=prime[i]-;
x/=prime[i];
while (x%prime[i]==)
{
ret*=prime[i];
x/=prime[i];
}
}
}
if (x>)
{
ret*=x-;
}
return ret;
}
int main()
{
freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
qword i;
scanf("%d",&n);
qword ans1=,ans2=;
// for (i=1;i<=n;i++)
// ans1+=gcd(i,n);
// printf("%d\n",ans1);
init();
qword l=ceil(sqrt(n));
for (i=;i*i<n;i++)
{
if (n%i==)
{
ans2+=(qword)i*phi(n/i);
ans2+=(qword)(n/i)*phi(i);
}
}
if (l*l==n)
{
ans2+=(qword)(n/l)*phi(l);
}
printf(LL"\n",ans2);
return ;
}
 

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

  3. bzoj 2705 [SDOI2012]Longge的问题——欧拉函数大水题

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2705 撕逼题.不就是枚举gcd==d,求和phi[ n/d ]么. 然后预处理sqrt (n ...

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

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

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

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

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

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

  7. [bzoj]2705: [SDOI2012]Longge的问题[数论][数学][欧拉函数][gcd]

    [bzoj]P2705 OR [luogu]P2303 Longge的问题 Description Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数N,你需 ...

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

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2705 [题目大意] 求出∑gcd(i,N)(1<=i<=N) [题解] $ ...

  9. BZOJ.2705.[SDOI2012]Longge的问题(莫比乌斯反演 欧拉函数)

    题目链接 \(Description\) 求\[\sum_{i=1}^n\gcd(i,n)\] \(Solution\) \[ \begin{aligned} \sum_{i=1}^n\gcd(i,n ...

随机推荐

  1. servlet和手动创建servlet,断点调试

    1.    什么是Servlet Servlet是一种用Java语言编写的Web应用组件 Servlet主要用于动态网页输出,扩展了Web服务器的功能 Servlet由Servlet容器进行管理 2. ...

  2. 必应代码搜索 Bing Code Search 安装

    微软这几天推出基于bing搜索引擎的  Bing Code Search ,可直接在浏览器上搜索和运行代码.目前中文版必应无法使用本功能,有需要的同学可以转到英文版进行搜索: 英文版必应:  http ...

  3. c# 值传递 引用传递

    以前一直误以为引用类型,在作为参数传递时,都是引用传递(类似于值传递中的ref),也就是说,把引用类型的变量作为参数传递给方法,在方法中修改该参数,会改变这个变量的值, 后来通过一些事例发现,上面的认 ...

  4. 父页面iframe自动适应子页面的宽高度

    <script type="text/javascript"> function load(){ parent.document.getElementById(&quo ...

  5. oracle 11g 安装及网络配置

    非原创,纯属笔记 安装:基本按照默认下一步安装的 1)可执行安装文件[ setup.exe ]双击安装 2):配置安全更新,取消下面的“我希望通过My Oracle Support接受安全更新(W)” ...

  6. php&nbsp;curl_init函数用法

    原文地址:curl_init函数用法">php curl_init函数用法作者:loading 使用PHP的cURL库可以简单和有效地去抓网页.你只需要运行一个脚本,然后分析一下你所抓 ...

  7. Java-Android 之应用停止错误

    在Android在手机上运行的时候: 经常会出现应用程序停止: 一: 因为触发的方法里面没有传值View 对象,方法报错

  8. C#语法糖之第六篇: 泛型委托- Predicate<T>、Func<T>

    今天继续分享泛型委托的Predicate<T>,上篇文章讲了Action委托,这个比Action委托功不一样的地方就是委托引用方法是Bool返回值的方法,Action为无返回值.首先我们看 ...

  9. DataList分页-增加自动编号列

    <asp:DataList ID="dl_XUDAXIA" runat="server"> <HeaderTemplate> <t ...

  10. [Twisted] transport

    transport代表网络上两个节点的连接.它描述了连接的具体细节,如TCP还是UDP. transports实现了ITransport接口,包含以下方法 write:以非阻塞的方式向连接写数据. w ...