题目链接:https://ac.nowcoder.com/acm/contest/221/I
前置知识:
- 欧拉函数 是1~n中与n互质的个数。
- ,是1~n内与n互质的数的和。
大意:求1~n之间与n不互质的数的和
思路:我们可以利用已有公式求出1~n内与n互质的数的和,然后用1~n的总和(总和为:)减去就是与n不互质的数的和。
可以通过欧拉筛求,但是因为常数n太大,所以对n进行质因数分解,然后用公式来求。
注意先对n取模再相乘。
Code
#include <bits/stdc++.h>
#define ll long long
#define pir pair<int, int>
#define pirl pair<ll, ll>
#define debug(x) cout << #x << ":" << x << "\n"
const int mod = 1e9 + 7;
const ll ds = 1e18;
const double eps = 1e-8;
using namespace std;
const int N = 1e3 + 5;
ll euler(ll x) {
ll res = x;
for (ll i = 2; i * i <= x; i++) {
if (x % i == 0) {
res = res / i * (i - 1);
while (x % i == 0) x /= i;
}
}
if (x > 1) res = res / x * (x - 1);
return res;
}
ll qpow(ll a,ll b) {
ll res = 1;
while(b) {
if(b&1) res = res*a%mod;
b >>= 1;
a = a*a%mod;
}
return res;
}
void solve() {
ll n;
while (cin >> n) {
ll ph = euler(n);
ph %= mod;
n %= mod;
ll ans = ((1+n)*n%mod-ph*n%mod+mod)%mod*qpow(2,mod-2)%mod;
cout << ans%mod << endl;
}
}
int main() {
// int T;
// cin >> T;
// while (T--)
solve();
system("exit");
return 0;
}