打表
Description
有一道比赛题目,输入两个整数x,y(1≤x,y≤n),输出某个函数f(x,y)。有位选手想打表(即事先计算出所有的f(x,y),写在源代码里),但是表太大了,原代码超过了比赛的限制,需要精简。
好在那道题目有一个性质,使得很容易根据f(x,y)算出f(x*k,y*k)(其中k是正整数),这样有一些f(x,y)就不需要存在表里了。输入n(n≤50000),你的任务是统计最简的表里有多少个元素。例如,n=2时有3个(1,1),(1,2),(2,1)。
Input
输入只有一行,一个整数n;
Output
输出也仅有一行,即表里元素的个数。
Sample Input
2
Sample Output
3
Source
欧拉函数
Solution
对于该函数的前一维x,与之配对的可选y(y < x)有phi[x]个,然后根据该函数的对称性随便搞一搞就出来了
Code
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
#include <queue>
#define L 50010
#define LL long long
using namespace std;
int n, phi[L];
LL ans;
int main(){
freopen("CJOJ2428.in", "r", stdin);
freopen("CJOJ2428.out", "w", stdout);
scanf("%d", &n);
phi[1] = 1;
for (int i = 2; i <= n; ++i)
if (!phi[i])
for (int j = i; j <= n; j += i) {
if (!phi[j]) phi[j] = j;
phi[j] = phi[j] / i * (i - 1);
}
for (int i = 2; i <= n; ++i) ans += phi[i];
printf("%lld\n", ans * 2 + 1);
return 0;
}