bzoj 1257: [CQOI2007]余数之和
题目大意:给你n,k, 让你求 k % 1 + k % 2 + k % 3 + ..... + k % n的值。
思路:我们能将k % i 转换成 k - (k / i) * i,原式变为 n * k - Σi * (k / i), 因为 k / i 是向下取整, 所以一段连续的i里边肯定
存在k / i 是一样的, 我们分段求和就好啦, 用二分找出下界。
1 #include<bits/stdc++.h> 2 #define LL long long 3 #define fi first 4 #define se second 5 #define mk make_pair 6 #define pii pair<int,int> 7 #define piii pair<int, pair<int,int>> 8 9 using namespace std; 10 11 const int N = 100 + 7; 12 const int M = 1e4 + 7; 13 const int inf = 0x3f3f3f3f; 14 const LL INF = 0x3f3f3f3f3f3f3f3f; 15 const int mod = 1e9 + 7; 16 17 LL n, k; 18 19 LL cal(LL val) { 20 LL l = 1, r = n, ans = -1; 21 while(l <= r) { 22 LL mid = l + r >> 1; 23 if(k / mid <= val) ans = mid, r = mid - 1; 24 else l = mid + 1; 25 } 26 return ans; 27 } 28 29 int main() { 30 scanf("%lld%lld", &n, &k); 31 LL ans = n * k; 32 33 LL R = n, L; 34 while(R) { 35 L = cal(k / R); 36 ans -= (L + R) * (R - L + 1) / 2 * (k / R); 37 R = L - 1; 38 } 39 printf("%lld\n", ans); 40 return 0; 41 } 42 /* 43 */