POJ2480:Longge's problem(欧拉函数的应用)
题目链接:传送门
题目需求:
Given an integer N(1 < N < 2^31),you are to calculate ∑gcd(i, N) 1<=i <=N. 这题就是上一篇博客的变形。
题目解析:首先先求出与N互质的个数,即N的欧拉函数值,之后分解出N的因子来,求解方法如下。
证明:
要求有多少个 i 满足gcd(i, N) = d 如果gcd(i, N) = d,则gcd(i/d, N/d) = 1 由于i <= N,所以 i/d <= N/d,转化为求多少个不大于N/d的数与N/d互质,而这就是欧拉函数 所以有phi(N/d)个 i 满足gcd(i, N) = d,所以∑d*phi(N/d)即为答案。
我搜了大部分人的题解都是利用乘性函数做的,思想我附在代码下面,因为已A,我就不用他们那种方法了。
代码:(欧拉函数打表204ms)
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
typedef long long ll;
using namespace std;
ll phi[];
ll f[];
ll sum,n,t,tt,i;
void init()
{
memset(phi,,sizeof(phi));
phi[]=;
for(int i=; i<=; i++)
{
if(!phi[i])
{
for(int j=i; j<=; j=j+i)
{
if(!phi[j]) phi[j]=j;
phi[j]-=phi[j]/i;
}
}
}
}
int main()
{
init();
while(scanf("%I64d",&n)!=EOF)
{
tt=;
for(i=; i*i<n; i++)
{
if(n%i==)
{
f[tt++]=i;
f[tt++]=n/i;
}
}
if(i*i==n)
f[tt++]=i;
sort(f,f+tt);
if(tt==)
{
tt=*n-;
printf("%I64d\n",tt);
continue;
}
t=n;
sum=n;
for(i=; i*i<=t; i++)
{
if(t%i==)
{
sum-=sum/i;
t/=i;
while(t%i==)
t/=i;
}
}
if(t!=)
sum-=sum/t;
sum+=n;
for(i=; i<tt; i++)
{
if(n/f[i]>=)
{
ll temp=n/f[i];
ll cot=temp;
for(ll z=; z*z<=temp; z++)
{
if(temp%z==)
{
t/=z;
cot-=cot/z;
while(temp%z==)
temp/=z;
}
}
if(temp!=) cot-=cot/temp;
sum+=cot*f[i];
continue;
}
sum+=(phi[n/f[i]])*f[i];
}
printf("%I64d\n",sum);
}
return ;
}
下面推导没看懂。。。。。
积性函数:若任取互质的两个数m,n,都有f(m*n) = f(m)*f(n),那么f就是积性函数
容易证明gcd(i,n)是积性函数,即: 如果n = m1*m2 且gcd(i,m1*m2) = gcd(i,m1)*gcd(i,m2). 然后根据具体数学上的结论: 积性函数的和也是积性的,所以如果我们设所求答案是f(n) 则: f(n) = f(m1)*(m2) 其中,m1*m2 = n 且m1,m2互质!
经过因子分解,那种只要求到f(p^k)就可以利用积性把所有结果相乘得到最后答案。
还要一个结论: f(n) = sum(p * phi(n/p)) 其中p是n的因子,phi(n/p) 是从1到n有多少个数和n的gcd是p, 这个结论比较好证明的。
所以求f(p^k)转化成求phi(p^i) i =0....k; 而根据公式phi(p^i) = (p-1)*p^(i-1)可以求出,这样整个问题就解决了。
POJ2480:Longge's problem(欧拉函数的应用)的更多相关文章
- poj 2480 Longge's problem [ 欧拉函数 ]
传送门 Longge's problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7327 Accepted: 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 ...
- poj 2480 Longge's problem 欧拉函数+素数打表
Longge's problem Description Longge is good at mathematics and he likes to think about hard mathem ...
- BZOJ 2705: [SDOI2012]Longge的问题 [欧拉函数]
2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 2553 Solved: 1565[Submit][ ...
- Bzoj 2705: [SDOI2012]Longge的问题 欧拉函数,数论
2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 1959 Solved: 1229[Submit][ ...
- 【bzoj2705】[SDOI2012]Longge的问题 欧拉函数
题目描述 Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N). 输入 一个整数,为N. 输出 ...
- BZOJ2705: [SDOI2012]Longge的问题(欧拉函数)
题意 题目链接 Sol 开始用反演推发现不会求\(\mu(k)\)慌的一批 退了两步发现只要求个欧拉函数就行了 \(ans = \sum_{d | n} d \phi(\frac{n}{d})\) 理 ...
- [SDOI2012] Longge的问题 - 欧拉函数
求 \(\sum\limits_{i=1}^{n}gcd(i,n)\) Solution 化简为 \(\sum\limits_{i|n}^{n}φ(\dfrac{n}{i})i\) 筛出欧拉函数暴力求 ...
- UVa 10837 A Research Problem 欧拉函数
题意: 给你一个欧拉函数值 phi(n),问最小的n是多少. phi(n) <= 100000000 , n <= 200000000 解题思路: 对于欧拉函数值可以写成 这里的k有可能是 ...
- Bzoj-2705 Longge的问题 欧拉函数
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2705 题意: 求 sigma(gcd(i,n), 1<=i<=n<2^3 ...
随机推荐
- 03 java基本
输入输出流 标准输出流比较简单, 只需要使用 System.out.println 就可以了, 标准输入流步骤如下: Scanner in = new Scanner(System.in); 然后可以 ...
- 除去Scala的糖衣(13) -- Default Parameter Value
欢迎关注我的新博客地址:http://cuipengfei.me/ 好久没有写博客了,上一次更新竟然是一月份. 说工作忙都是借口,咋有空看美剧呢. 这半年荒废掉博客说到底就是懒,惯性的懒惰.写博客这事 ...
- java---sychronized的深入理解
synchronized 关键字,代表这个方法加锁,相当于不管哪一个线程A每次运行到这个方法时,都要检查有没有其它正在用这个方法的线程B(或者C D等),有的话要等正在使用这个方法的线程B(或者C D ...
- 全新的membership框架Asp.net Identity——绕不过的Claims
http://www.cnblogs.com/JustRun1983/p/4708176.html?utm_source=tuicool&utm_medium=referral
- WPF 的拖拽操作(DragDrop)
在WPF中似乎没有对拖拽操作进行改变,和以前的方式一样.如果曾近在 Windows 窗体应用程序中使用过鼠标拖放,就会发现在 WPF 中的编程接口实际上没有发生变化.重要的区别是用于拖放操作的方法和事 ...
- 模板,BFS
#include <stdio.h> #include <string.h> #include <queue> using namespace std; struc ...
- swift - UIView 设置背景色和背景图片
代码如下: let page = UIView() page.frame = self.view.bounds //直接设置颜色 page.backgroundColor = UIColor.gree ...
- 创建4个线程,两个对j加一,两个对j减一(j两同两内)
package multithread; public class MyThread { //j变量私有 private int j; //同步的+1方法 private synchronized v ...
- Struts 2再曝远程代码执行漏洞S2-037
导读今年4月份,Apache Stuts 2之上发现的S2-033远程代码执行漏洞,以迅雷不及掩耳之势席卷而来.其利用代码很快就在短时间内迅速传播.而且官方针对这个高危漏洞的修复方案还是无效的. 悲剧 ...
- Openstack实现共有云VPC的SDN网络
Neutron的第二个网络模型,自服务网络 参考官方文档:https://docs.openstack.org/newton/zh_CN/install-guide-rdo/neutron-contr ...