NC229652. Problem J. mesh
描述
输入描述
Enter a line containing two positive integers
输出描述
Output a positive integer to represent the answer.
示例1
输入:
7 7
输出:
62
示例2
输入:
123 456
输出:
125162
示例3
输入:
114514 1919810
输出:
610236659
示例4
输入:
88888888 888888888
输出:
207026669
C++ 解法, 执行用时: 312ms, 内存消耗: 45836K, 提交时间: 2021-11-01 22:24:11
#include<bits/stdc++.h> #include<unordered_map> using namespace std; const int mod = 1e9 + 7; const int maxn = 5000001; int pri[maxn], cnt; bool vis[maxn]; int f[maxn], g[maxn]; void init() { f[1] = g[1] = 1; for (int i = 2; i < maxn; i++) { if (!vis[i]) pri[cnt++] = i, f[i] = i - 1, g[i] = i - 2; for (int j = 0; j < cnt && i * pri[j] < maxn; j++) { vis[i * pri[j]] = 1; if (i % pri[j]) f[i * pri[j]] = f[i] * f[pri[j]], g[i * pri[j]] = g[i] * g[pri[j]]; else { f[i * pri[j]] = f[i] * pri[j]; if (i / pri[j] % pri[j]) g[i * pri[j]] = g[i / pri[j]] * f[pri[j]] * f[pri[j]]; else g[i * pri[j]] = g[i] * pri[j]; break; } } } for (int i = 2; i < maxn; i++) { f[i] += f[i - 1],g[i]+=g[i-1]; if (f[i] >= mod) f[i] -= mod; if (g[i] >= mod) g[i] -= mod; } } map<int, int>mf, mg; int djsf(int n) { if (n < maxn) return f[n]; if (mf.count(n)) return mf[n]; long long ans = 1ll * n * (n + 1) / 2 % mod; for (int l = 2, r; l <= n; l = r + 1) r = n / (n / l), ans -= 1ll * (r - l + 1) * djsf(n / l) % mod; return mf[n] = ans % mod; } int djsg(int n) { if (n < maxn) return g[n]; if (mg.count(n)) return mg[n]; long long ans = djsf(n); for (int l = 2, r; l <= n; l = r + 1) r = n / (n / l), ans -= 1ll * (r - l + 1) * djsg(n / l) % mod; return mg[n] = ans % mod; } signed main() { init(); int n, m; cin >> n >> m; long long ans = 0; for (int l = 1, r; l <= min(n, m); l = r + 1) r = min(n / (n / l), m / (m / l)), ans += 1ll * n / l * (m / l) % mod * (djsg(r) - djsg(l - 1)) % mod; cout << (ans % mod + mod) % mod; }