P2261 [CQOI2007]余数求和(数论分块)

传送门

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <vector>
 5 #include<set>
 6 #include <map>
 7 #include <string>
 8 using namespace std;
 9 
10 #define ll long long
11 #define pb push_back
12 
13 const ll MOD = 1e9 + 7;
14 const int N = 1e6;
15 
16 
17 // ∑(k - k / i * i) -> nk - ∑(k / i * i)
18 
19 void solve()
20 {   
21     ll n, k;
22     cin >> n >> k;
23     
24     ll ans1 = n * k;
25     ll ans2 = 0;
26     ll l, r;
27 
28     for(l = 1, r = 0; l <= n; l = r + 1){
29         if(l > k) break;
30         r = min(n, k / (k / l));
31         ans2 += (r - l + 1) * (r + l) / 2 * (k / l);
32     }
33     cout << ans1 - ans2 << endl;
34 }
35 
36 int main()
37 {   
38     ios::sync_with_stdio(false);
39     cin.tie(0);
40     cout.tie(0);
41     solve();
42     //cout << "ok" << endl;
43     return 0;
44 }

 

posted @ 2020-08-04 21:33  SummerMingQAQ  阅读(174)  评论(0编辑  收藏  举报