POJ 2480 Longge's problem 积性函数
题目来源: id=2480" style="color:rgb(106,57,6); text-decoration:none">POJ 2480 Longge's problem
题意:求i从1到n的gcd(n, i)的和
思路:首先假设m, n 互质 gcd(i, n*m) = gcd(i, n)*gcd(i, m) 这是一个积性函数积性函数的和还是积性函数
由欧拉函数知识得 phi(p^a) = p^a - p^(a-1) p是素数 a是正整数
得到终于答案f(n) = f(p1^a1)*f(p2^a2)*...*f(pn^an) 当中f(p^a) = a*(p^a-p^(a-1))+p^a
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL; const int maxn = 1000010;
//筛素数
int vis[maxn];
LL prime[maxn]; LL pow_mod(LL a, LL p)
{
LL ans = 1;
while(p)
{
if(p&1)
{
ans *= a; }
a *= a; p >>= 1;
}
return ans;
} void sieve(int n)
{
int m = sqrt(n+0.5);
memset(vis, 0, sizeof(vis));
vis[0] = vis[1] = 1;
for(int i = 2; i <= m; i++)
if(!vis[i])
for(int j = i*i; j <= n; j += i)
vis[j] = 1;
} int get_primes(int n)
{
sieve(n);
int c = 0;
for(int i = 2; i <= n; i++)
if(!vis[i])
prime[c++] = i;
return c;
} int main()
{
int c = get_primes(200000);
int cas = 1;
int T;
LL n, ans;
while(scanf("%I64d", &n) != EOF)
{
ans = 1;
for(int i = 0; i < c && prime[i]*prime[i] <= n; i++)
{
if(n%prime[i] == 0)
{
LL sum = 0;
while(n%prime[i] == 0)
{
sum++;
n /= prime[i]; }
ans *= sum*(pow_mod(prime[i], sum)-pow_mod(prime[i], sum-1))+pow_mod(prime[i], sum);
}
}
if(n > 1)
{
ans *= n-1+n;
}
printf("%I64d\n", ans);
}
return 0;
}
POJ 2480 Longge's problem 积性函数的更多相关文章
- poj 2480 Longge's problem 积性函数性质+欧拉函数
题意: 求f(n)=∑gcd(i, N) 1<=i <=N. 分析: f(n)是积性的数论上有证明(f(n)=sigma{1<=i<=N} gcd(i,N) = sigma{d ...
- poj 2480 Longge's problem 积性函数
思路:首先给出几个结论: 1.gcd(a,b)是积性函数: 2.积性函数的和仍然是积性函数: 3.phi(a^b)=a^b-a^(b-1); 记 f(n)=∑gcd(i,n),n=p1^e1*p2^e ...
- POJ 2480 Longge's problem (积性函数,欧拉函数)
题意:求∑gcd(i,n),1<=i<=n思路:f(n)=∑gcd(i,n),1<=i<=n可以知道,其实f(n)=sum(p*φ(n/p)),其中p是n的因子.为什么呢?原因 ...
- POJ_2480 Longge's problem【积性函数+欧拉函数的理解与应用】
题目: Longge is good at mathematics and he likes to think about hard mathematical problems which will ...
- 题解报告:poj 2480 Longge's problem(欧拉函数)
Description Longge is good at mathematics and he likes to think about hard mathematical problems whi ...
- poj 2480 Longge's problem
/** 大意: 计算f(n) = ∑ gcd(i, N) 1<=i <=N. 思路: gcd(i,x*y) = gcd(i,x) * gcd(i, y ) 所以gcd 为积性函数 又因为积 ...
- Problem : 这个题如果不是签到题 Asm.Def就女装(积性函数dp
https://oj.neu.edu.cn/problem/1460 思路:若n=(p1^a1)*(p2^a2)...(pn^an),则f(n,0)=a1*a2*...*an,显然f(n,0)是积性函 ...
- TopCoder SRM 660 Div2 Problem 1000 Powerit (积性函数)
令$f(x) = x^{2^{k}-1}$,我们可以在$O(k)$的时间内求出$f(x)$. 如果对$1$到$n$都跑一遍这个求解过程,时间复杂度$O(kn)$,在规定时间内无法通过. 所以需要优化. ...
- luogu4464:莫比乌斯反演,积性函数和伯努利数
题目链接:https://www.luogu.com.cn/problem/P4464 简记$gcd(x,y)=(x,y)$. 推式子: $\sum_{i=1}^{n}{(i,n)^xlcm(i,n) ...
随机推荐
- spring执行事务提交后进行一些逻辑操作
在使用spring事务时,我们通常会把事务内的所有操作当成是一个原子操作.也就是当整个事务内的所有代码都执行完成后, 才会将所有的数据落实到数据库中.这样做有时也会给我们造成麻烦.比如以下场景: 根据 ...
- 【CF1023B】Pair of Toys(解方程)
题意:给定n个玩具要你选出两个玩具求出k的价值,第i个玩具的价值为i.若是没有选择方案,输出0 补充:玩具A与玩具B 和 玩具B和玩具A 是同一种选择 n,k<=1e14 思路:列出式子,解不等 ...
- 自己写的微信小程序炸金花简单版
app.json: { "pages":[ "pages/index/index" ], "window":{ "navigati ...
- http协议相关面试题
浏览器输入url按回车背后经历了哪些? 1.在PC浏览器的地址栏输入一串URL,然后按Enter键这个页面渲染出来,这个过程中都发生了什么事? 1.首先,在浏览器地址栏中输入url,先解析url,检测 ...
- 驱动12.移植dm9000驱动程序
1 确定相异性 1.1 选中网卡芯片nGCS4 1.2 确定相异性:基地址,中断号,设置时序(内存控制器BWSCON,BANKCONn) 1.3 修改相应的部分 2 测试DM9000C驱动程序:2.1 ...
- Drupal service module 介绍
https://www.ostraining.com/blog/drupal/services/ https://www.drupal.org/node/1246470 https://www.dru ...
- POJ 3368.Frequent values-处理数据+RMQ(ST)
昨天写的博客删了,占坑失败,还是先把RMQ玩的6一点再去搞后面的东西.废话少说,题解题姐姐_(:з」∠)_ Frequent values Time Limit: 2000MS Memo ...
- Light oj 1085 - All Possible Increasing Subsequences (简单dp + 离散化 + BIT)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1085 题意: 问你有多少个上升子序列. 思路: dp[i]表示以第i个数结尾的 ...
- Nginx三种模式的虚拟主机(附Apache基于域名的虚拟主机)
1.安装nginx # pcre中文"perl兼容正则表达式",安装pcre库是为了让nginx支持具备URL重写功能 # 的Rewrite模块,rewrite可以实现动态页面转成 ...
- Codeforces Gym 100203E bits-Equalizer 贪心
原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 考虑到交换可以减少一次操作,那么可以 ...