求 n % 1 + n %2 + n % 3 .....n % n 。结果取余1e9 + 7
n <= 1e12
显然是用到了整除分块。
这里的for循环可以当作模板
for (ll l = 1, r; l <= n; l = r + 1) { if (n / l != 0) r = min(n / (n / i), n); else r = n; // do something }
当然,此题还要注意取模
#pragma warning(disable:4996) #include<iostream> #include<algorithm> #include<bitset> #include<tuple> #include<unordered_map> #include<fstream> #include<iomanip> #include<string> #include<cmath> #include<cstring> #include<vector> #include<map> #include<set> #include<list> #include<queue> #include<stack> #include<sstream> #include<cstdio> #include<ctime> #include<cstdlib> #define INF 0x3f3f3f3f #define inf 0x7FFFFFFF #define moD 1000000003 #define pii pair<int,string> #define eps 1e-8 #define equals(a,b) (fabs(a-b)<eps) #define bug puts("bug") #define re register #define fi first #define se second const int maxn = 1e6 + 5; const double Inf = 10000.0; const double PI = acos(-1.0); typedef long long ll; typedef unsigned long long ull; using namespace std; const ll MOD = 1e9 + 7; ll inv = 5e8 + 4; int main() { ll n; scanf("%lld", &n); ll res = (n % MOD) * (n % MOD) % MOD; ll ans = 0; for (ll l = 1, r; l <= n; l = r + 1) { if (n / l != 0) r = min(n / (n / l), n); else r = n; ans += ((n / l) % MOD) * ((((l + r) % MOD) * ((r - l + 1) % MOD) % MOD) * inv % MOD); ans %= MOD; } printf("%lld", ((res - ans + MOD) % MOD + MOD) % MOD); }