就是欧拉函数的应用,求2-n的每个数的欧拉函数的2倍+1,用了三种方法,前两种是一个一个求,第一种卡着时间过的,第二种预处理之后就很快了,
第三种用的求连续的1-n内的每个数的欧拉函数的模版求的,非常快
//============================================================================
// Name : 10820.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int n, ans;
int euler_phi(int n){
int ans = n;
for(int i = 2;i*i <= n;i++){
if(n%i == 0){
ans -= ans/i;
while(n%i == 0)
n/=i;
}
}
if(n>1)
ans -= ans/n;
return ans;
}
int main() {
freopen("a.txt", "r", stdin);
while(scanf("%d", &n)&&n){
ans = 1;
for(int i = 2;i <= n;i++)
ans += euler_phi(i)*2;
printf("%d\n", ans);
}
return 0;
}
//============================================================================
// Name : 10820.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int n, ans[50005];
int euler_phi(int n){
int ans = n;
for(int i = 2;i*i <= n;i++){
if(n%i == 0){
ans -= ans/i;
while(n%i == 0)
n/=i;
}
}
if(n>1)
ans -= ans/n;
return ans;
}
int main() {
freopen("a.txt", "r", stdin);
ans[1] = 1;
for(int i = 2;i <= 50002;i++)
ans[i] = ans[i-1]+euler_phi(i)*2;
while(scanf("%d", &n)&&n){
printf("%d\n", ans[n]);
}
return 0;
}
//============================================================================
// Name : 10820.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int n, ans[50005], phi[50005];
void phi_table(int n)
{
for (int i = 2; i <= n; i++)
phi[i] = 0;
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);
}
}
}
}
int main() {
freopen("a.txt", "r", stdin);
phi_table(50002);
ans[1] = 1;
for(int i = 2;i <= 50002;i++){
ans[i] = ans[i-1]+phi[i]*2;
}
while(scanf("%d", &n)&&n){
printf("%d\n", ans[n]);
}
return 0;
}