# HDU 5780 (欧拉函数)

## 题目链接:点击这里

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
#define maxn 1000005
#define INF 1e18
#define mod 1000000007

bool vis[maxn];
int phi[maxn], prime[maxn], cnt;
long long sum[maxn];//[1,n]中有多少对数互质
int fac[maxn];//每一个数的一个素因子
int n, x;
long long f[maxn];//逆元

void init () {
f[1] = 1;
for (int i = 2; i <= 1000000; i++){
f[i] = (long long)(mod - mod/i) * f[mod % i] % mod;
}
cnt = 0;
memset (vis, 0, sizeof vis);
phi[1] = 1;
for (int i = 2; i < maxn; i++) {
if (!vis[i]) {
prime[cnt++] = i;
fac[i] = i;
phi[i] = i-1;
}
for (int j = 0; j < cnt; j++) {
if (1LL*i*prime[j] >= maxn) break;
vis[i*prime[j]] = 1;
fac[i*prime[j]] = prime[j];
if (i%prime[j] == 0) {
phi[i*prime[j]] = phi[i] * prime[j];
break;
}
else {
phi[i*prime[j]] = phi[i] * (prime[j]-1);
}
}
}
sum[1] = 1;
for (int i = 2; i < maxn; i++) {
sum[i] = sum[i-1];
sum[i] = (sum[i-1] + 1LL*phi[i]*2) % mod;
}
}

long long qpow (long long a, long long b) {
if (b == 0)
return 1;
long long ans = qpow (a, b>>1);
ans = ans*ans % mod;
if (b&1)
ans = ans*a%mod;
return ans;
}

long long solve () {
long long tmp = 1;
long long ans = 0;
int j;
for (int i = 1; i <= n; i = j+1) {
tmp *= x; tmp %= mod;
j = (n/(n/i));
long long gg =  qpow (x, j-i+1);
ans += tmp * (gg+mod-1) % mod * sum[n/i] % mod * f[x-1] % mod;
tmp = tmp * gg % mod * f[x] % mod;
ans %= mod;
}
ans -= 1LL*n*n;
ans = (ans % mod + mod) % mod;
return ans;
}

int main () {
init ();
int t;
scanf ("%d", &t);
while (t--) {
scanf ("%d%d", &x, &n);
printf ("%lld\n", solve ());
}
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120