欧拉函数 或 积性函数应用。显然,我们设L = n / gcd(i, n) ,t = i /gcd(i, n)。则gcd(L, t) = 1;于是,gcd(i, n) 的个数为phi(L)。于是题目就可以用欧拉函数求解了。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<map>
#include<set>
#define LL long long
#define CLR(a, b) memset(a, b, sizeof(a))
#define REP(i, n) for(int i = 0; i < (n); i ++)
using namespace std;
const int N = 100100;
int phi[N];
bool isp[N];
vector<int> p;
void get_P()
{
CLR(isp, true);p.clear();
for(int i = 2; i < N; i ++)
{
if(isp[i])
{
p.push_back(i);
if(i < 1111) for(int j = i * i; j < N; j += i)
{
isp[j] = false;
}
}
}
}
LL Euler_phi(LL n)
{
LL ret = n;
for(int i = 0; (LL)p[i] * p[i] <= n; i ++) if(n % p[i] == 0)
{
ret = ret / p[i] * (p[i] - 1);
while(n % p[i] == 0) n /= p[i];
}
if(n > 1) ret = ret / n * (n - 1);
return ret;
}
void get_phi()
{
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()
{
LL ans, n;get_phi();get_P();
while(scanf("%I64d", &n) != EOF)
{
ans = 0;
for(int i = 1; (LL)i * i <= n; i ++)
{
if(n % i != 0) continue;
ans += i * Euler_phi(n / i);
if(i != n / i)
ans += (n / i) * phi[i];
}
printf("%I64d\n", ans);
}
}
f(n) = f(p1^a1) * f(p2^a2)*...*f(pn^an)。 对于f(pi^ai)就挺容易求了,最后可以求得f(pi^ai) = ai * (pi^ai - pi^(ai - 1)) + pi^ai;
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<map>
#include<set>
#define LL long long
#define CLR(a, b) memset(a, b, sizeof(a))
#define REP(i, n) for(int i = 0; i < (n); i ++)
using namespace std;
const int N = 111111;
vector<LL> hav, gx;
vector<int> cnt, p;
bool isp[N];
void get_P()
{
isp[0] = isp[1] = 1;
for(int i = 2; i < N; i ++)
{
if(!isp[i])
{
p.push_back(i);
if(i <= 1111)for(int j = i * i; j < N; j += i) isp[j] = 1;
}
}
}
void get_hav(LL h)
{
hav.clear();cnt.clear();gx.clear();
for(int i = 0; (LL)p[i] * p[i] <= h; i ++)
{
if(h % p[i] == 0)
{
hav.push_back(p[i]);
LL tmp = 1;int ct = 0;
while(h % p[i] == 0)
{
h /= p[i];
tmp *= p[i];
ct ++;
}
gx.push_back(tmp);
cnt.push_back(ct);
}
}
if(h != 1)
{
hav.push_back(h);
cnt.push_back(1);
gx.push_back(h);
}
}
int main()
{
LL n, ans;get_P();
while(scanf("%I64d", &n) != EOF)
{
ans = 1;get_hav(n);
for(int i = 0; i < hav.size(); i ++)
{
ans *= cnt[i] * (gx[i] - gx[i] / hav[i]) + gx[i];
}
printf("%I64d\n", ans);
}
}