思路 : 欧拉函数打表(1~~n)
随便找一组数据: n=4
(1,1) (1,2) (1,3) (1,4)
(2,1) (2,2) (2,3) (2,4)
(3,1) (3,2) (3,3) (3,4)
(4,1) (4,2) (4,3) (4,4)
会发现这是关于对角线(11)(22)(33)(44)对称的
因此只用求左下半部分的就可以 然后把每行的欧拉函数相加(因为对称 所以*2)
第一行求1的euler
第二行求2的euler
第三行求3的euler
。。。。。。
code:
import java.util.Scanner;
public class Main {
static int e[] = new int[50005], maxn = 50001;
static long v[] = new long[50005];
public static void main(String[] args) {
euler();
Scanner sc = new Scanner(System.in);
while(sc.hasNextInt()){
int n = sc.nextInt();
if(n == 0)
break;
System.out.println(v[n]);
}
}
static void euler(){
e[1] = 1;
for(int i =2; i<=maxn; i++){
if(e[i] == 0)
for(int j = i; j<=maxn; j+=i){
if(e[j] == 0) e[j] = j;
e[j] = e[j]/i*(i-1);
}
}
v[1] = 1;
for(int i = 2; i<=maxn; i++)
v[i] = 2*e[i] + v[i-1];
}
}