CF1542C-Strange Function
题目大意:定义f(x)为“不能被x整除的最小正整数”,给定n,求
i=1∑nf(i)
如果f(i)=x,那么i是1,2,3,4,5,...,x−1这x-1个数的公倍数,即这x-1个数的最小公倍数的若干倍。假设这x-1个数的最小公倍数为lcm,那么总共的n个数中一共有n÷lcm个这x-1个数的公倍数,那么这x-1个数的f值都至少为x。
所以可以写出代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #include <bits/stdc++.h> using namespace std; long long lcm(long long x, long long y) { return x / __gcd(x, y) * y; } int main() { int _; long long x, sum, res, i; const long long mod = 1e9 + 7; scanf("%d", &_); while (_--) { scanf("%lld", &x); for (sum = 2, i = 2, res = x << 1; sum <= x; sum = lcm(sum, i)) res = (res + x / sum) % mod, i++; printf("%lld\n", res); } return 0; }
|