UVA 11426 - GCD - Extreme (II)

题目链接

题意:给定N。求∑i<=ni=1∑j<nj=1gcd(i,j)的值。

思路:lrj白书上的例题,设f(n) = gcd(1, n) + gcd(2, n) + ... + gcd(n - 1, n).这种话,就能够得到递推式S(n) = f(2) + f(3) + ... + f(n) ==> S(n) = S(n - 1) + f(n);.

这样问题变成怎样求f(n).设g(n, i),表示满足gcd(x, n) = i的个数,这样f(n) = sum{i * g(n, i)}. 那么问题又转化为怎么求g(n, i),gcd(x, n) = i满足的条件为gcd(x / i, n / i) = 1,因此仅仅要求出欧拉函数phi(n / i),就能够得到与x / i互质的个数,从而求出gcd(x , n) = i的个数,这样总体就能够求解了

代码:

#include <stdio.h>
#include <string.h> const int N = 4000005; int n;
long long phi[N], s[N], f[N]; int main() {
phi[1] = 1;
for (int i = 2; i < N; i++) {
if (phi[i]) continue;
for (int j = i; j < N; j += i) {
if (!phi[j]) phi[j] = j;
phi[j] = phi[j] / i * (i - 1);
}
}
for (int i = 1; i < N; i++) {
for (int j = i * 2; j < N; j += i) {
f[j] += phi[j / i] * i;
}
}
s[2] = f[2];
for (int i = 3; i < N; i++)
s[i] = s[i - 1] + f[i];
while (~scanf("%d", &n) && n) {
printf("%lld\n", s[n]);
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

UVA 11426 - GCD - Extreme (II) (数论)的更多相关文章

  1. UVA 11426 GCD - Extreme (II) (数论|欧拉函数)

    题意:求sum(gcd(i,j),1<=i<j<=n). 思路:首先能够看出能够递推求出ans[n],由于ans[n-1]+f(n),当中f(n)表示小于n的数与n的gcd之和 问题 ...

  2. UVA 11426 GCD - Extreme (II) (欧拉函数)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Problem JGCD Extreme (II)Input: Standard ...

  3. UVa 11426 - GCD - Extreme (II)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  4. UVA 11426 GCD - Extreme (II) (欧拉函数+筛法)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70017#problem/O 题意是给你n,求所有gcd(i , j)的和,其中 ...

  5. UVA 11426 GCD - Extreme (II) (欧拉函数)题解

    思路: 虽然看到题目就想到了用欧拉函数做,但就是不知道怎么做... 当a b互质时GCD(a,b)= 1,由此我们可以推出GCD(k*a,k*b)= k.设ans[i]是1~i-1与i的GCD之和,所 ...

  6. UVA 11426 GCD - Extreme (II)(欧拉函数打表 + 规律)

    Given the value of N, you will have to find the value of G. The definition of G is given below:Here ...

  7. uva 11426 GCD - Extreme (II) (欧拉函数打表)

    题意:给一个N,和公式 求G(N). 分析:设F(N)= gcd(1,N)+gcd(2,N)+...gcd(N-1,N).则 G(N ) = G(N-1) + F(N). 设满足gcd(x,N) 值为 ...

  8. UVa 11426 - GCD - Extreme (II) 转化+筛法生成欧拉函数表

    <训练指南>p.125 设f[n] = gcd(1, n) + gcd(2, n) + …… + gcd(n - 1, n); 则所求答案为S[n] = f[2]+f[3]+……+f[n] ...

  9. UVA 11426 - GCD - Extreme (II) 欧拉函数-数学

    Given the value of N, you will have to find the value of G. The definition of G is given below:G =i< ...

随机推荐

  1. 同台交换机同样VLAN能够通信,不同VLAN不可通信

    一.示意图 二.IP规划 PC0:192.168.0.100  255.255.255.0        PC1:192.168.0.110  255.255.255.0        PC2:192 ...

  2. Android-管理Activity生命周期 -暂停和恢复一个Activity

    在正常的使用app时,前台的activity有时候会被可见的组件阻塞导致activity暂停.比如,当打开一个半透明的activity(就像打开了一个对话框),之前的activity就会暂停.只要ac ...

  3. POJ3061 Subsequence(二进制前缀和法律+仿真足)

    二分法+前缀和法律 满足子序列长度的条件(0,n)之间,sum[x+i]-sum[i]从i元素开始序列长度x和.前缀和可在O(n)的时间内统计 sum[i]的值.再用二分找出满足条件的最小的子序列长度 ...

  4. 买面包和IoC

    今天上午准备去一个阿姨,在那里买面包.这可能是由于小尺寸她的,因此,管理不规范,所以,当你买面包.没有人行.即使所有的大学生,似几乎没有这种意识.. . 但让我感到震惊的是.尽管没有排队,但阿姨似乎能 ...

  5. POJ 2112 Optimal Milking (二分 + floyd + 网络流)

    POJ 2112 Optimal Milking 链接:http://poj.org/problem?id=2112 题意:农场主John 将他的K(1≤K≤30)个挤奶器运到牧场,在那里有C(1≤C ...

  6. InputMonitor注意事项

    文章只记录自己的点点理解.为了你自己的参考. 1.mInputFocus WMS.addWindow()-->WMS.finishUpdateFocusedWindowAfterAssignLa ...

  7. leetcode文章137称号-Single Number II

    #include<stdio.h> #include<stdlib.h> int singleNumber(int* nums, int numsSize) { int cou ...

  8. How to pause the game in Uniy3D

    static float timeScale; Description The scale at which the time is passing. This can be used for slo ...

  9. dba_dependencies查询结果视图

    [oracle@rhel63single ~]$ sqlplus / as sysdba SQL*Plus: Release 11.2.0.4.0 Production on Fri Mar 13 0 ...

  10. C# 判断文件的真实格式

    为了防止图片木马,通过后缀判断文件的格式是不准确的.可以通过这种方式进行判断. static void Main(string[] args) { string path = @"C:\Us ...