https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2421
gcd(x,4) f(4)=3,pos[4]=3=1*3;
gcd(2x,8) f[4]=3],pos[8]=6=2*3;
....................................;
pos[i*j]=pos[i]*j;
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <vector> #include <cstdlib> #include <iomanip> #include <cmath> #include <ctime> #include <map> #include <set> using namespace std; #define lowbit(x) (x&(-x)) #define max(x,y) (x>y?x:y) #define min(x,y) (x<y?x:y) #define MAX 100000000000000000 #define MOD 1000000007 #define pi acos(-1.0) #define ei exp(1) #define PI 3.141592653589793238462 #define INF 0x3f3f3f3f3f #define mem(a) (memset(a,0,sizeof(a))) typedef long long ll; ll vis[4000006],pos[4000006],n; void init() { memset(pos,0,sizeof(pos)); for(int i=1;i<=4000001;i++) vis[i]=i; for(int i=2;i<=4000001;i++) { if(vis[i]==i) { for(int j=1;j*i<4000006;j++) { vis[j*i]=vis[j*i]-vis[j*i]/i; } } for(int j=1;j*i<4000006;j++) { pos[j*i]+=j*vis[i];//倍数关系 } } } int main() { init(); while(scanf("%lld",&n)&&n) { ll ans=0; for(int i=1;i<=n;i++) { ans+=pos[i]; } printf("%lld ",ans); } }