题目来源:

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&#39;s problem 积性函数的更多相关文章

  1. poj 2480 Longge&#39;s problem 积性函数性质+欧拉函数

    题意: 求f(n)=∑gcd(i, N) 1<=i <=N. 分析: f(n)是积性的数论上有证明(f(n)=sigma{1<=i<=N} gcd(i,N) = sigma{d ...

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

  3. 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的因子.为什么呢?原因 ...

  4. POJ_2480 Longge's problem【积性函数+欧拉函数的理解与应用】

    题目: Longge is good at mathematics and he likes to think about hard mathematical problems which will ...

  5. 题解报告:poj 2480 Longge's problem(欧拉函数)

    Description Longge is good at mathematics and he likes to think about hard mathematical problems whi ...

  6. poj 2480 Longge's problem

    /** 大意: 计算f(n) = ∑ gcd(i, N) 1<=i <=N. 思路: gcd(i,x*y) = gcd(i,x) * gcd(i, y ) 所以gcd 为积性函数 又因为积 ...

  7. 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)是积性函 ...

  8. TopCoder SRM 660 Div2 Problem 1000 Powerit (积性函数)

    令$f(x) = x^{2^{k}-1}$,我们可以在$O(k)$的时间内求出$f(x)$. 如果对$1$到$n$都跑一遍这个求解过程,时间复杂度$O(kn)$,在规定时间内无法通过. 所以需要优化. ...

  9. luogu4464:莫比乌斯反演,积性函数和伯努利数

    题目链接:https://www.luogu.com.cn/problem/P4464 简记$gcd(x,y)=(x,y)$. 推式子: $\sum_{i=1}^{n}{(i,n)^xlcm(i,n) ...

随机推荐

  1. 【Luogu】P2596书架(Splay)

    题目链接 通过这题我加深了对Splay的理解,原来Splay的子树也是可以接来接去接到别的点上的,而不是只能旋转qwq 具体接的办法就是swap大法. 对于Top操作我们把当前节点Splay到根,然后 ...

  2. [luoguP1251] 餐巾计划问题(费用流)

    传送门 模型 网络优化问题,用最小费用最大流解决. 实现 把每天分为二分图两个集合中的顶点Xi,Yi,建立附加源S汇T. 1.从S向每个Xi连一条容量为ri,费用为0的有向边. 2.从每个Yi向T连一 ...

  3. 如何用1个小时了解JSON

    W3school ↑↑↑学这个,1个小时够了.下面是节选: 代码例子1: <html> <body> <h2>在 JavaScript 中创建 JSON 对象< ...

  4. 基于Redis Sentinel的Redis集群(主从&Sharding)高可用方案

    本文主要介绍一种通过Jedis&Sentinel实现Redis集群高可用方案,该方案需要使用Jedis2.2.2及以上版本(强制),Redis2.8及以上版本(可选,Sentinel最早出现在 ...

  5. xsy 1836 - Shop

    from NOIP2016模拟题36 Description 商店里有n种背包和m种物品,物品体积为1到m,背包容积<=m 给出n个背包的容积 现在要求出这样一个物品集合,满足: 1)对于任意一 ...

  6. python算法与数据结构-顺序表(37)

    1.顺序表介绍 顺序表是最简单的一种线性结构,逻辑上相邻的数据在计算机内的存储位置也是相邻的,可以快速定位第几个元素,中间不允许有空,所以插入.删除时需要移动大量元素.顺序表可以分配一段连续的存储空间 ...

  7. JSTL <C:if></C:if> 和<C:ForEach></C:ForEach> 入门级~

    一.<C:If>标签:条件判断语句 <c:if test="${objList.nodetype == 1}">上级节点</c:if>   te ...

  8. Python入门--18--异常与try,except语句

    Python标准异常总结 AssertionError 断言语句(assert)失败 AttributeError 尝试访问未知的对象属性 EOFError 用户输入文件末尾标志EOF(Ctrl+d) ...

  9. phpcms V9 安装黄页模块后,注册为普通会员并登录,点立即免费入驻企业库出现白板

    解决步骤: 1. 将php.ini修改: display_errors = On 2. 再次尝试,显示出错误: Fatal error: require(): Failed opening requi ...

  10. git 中遇到的问题

    刚创建的github版本库,在push代码时出错: $ git push -u origin masterTo git@github.com:******/Demo.git ! [rejected] ...