2020牛客暑期多校训练营(第七场) H Dividing

Dividing

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define inf64 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
const int mod = 1e9+7;
const int maxn = 1e5+10;
/*
题解:
通过对(1,k) 的两种变形,可以得到只要式子如同 (1+x*k,k) (x*k,k)
就可以满足条件,然后容易发现就是枚举k n/k  和  (n-1)/k
但是这个k无法枚举,看这个式子容易想到数论分块,所以可以用数论分块来求。
*/

ll solve(ll n,ll k){
    ll l,r,ans = 0;
    ll len = min(n,k);
    for(l = 1,r = 0;l<=len;l=r+1){
        r = n/(n/l);
        if(r>len) break;
        ll x = (r-l+1)%mod;
        ll y = (n/l)%mod;
        ans = (ans + x*y%mod)%mod;
    }
    r = min(r*1ll,len);
    ll x = (r-l+1)%mod;
    ll y = (n/l)%mod;
    ans = (ans+ max(0ll,x)*y%mod)%mod;
    return ans;
}

int main(){
    ll n,k,ans = 0;
    scanf("%lld%lld",&n,&k);
    ans = (solve(n,k)+solve(n-1,k))%mod;
    ans = (ans - n%mod + mod)%mod;
    ans = (ans + k)%mod;
    printf("%lld\n", ans);
    return 0;
}
上一篇:A. Integer Sequence Dividing---简单数学Codeforces Round #531 (Div. 3)


下一篇:A. Digits Sequence Dividing---Educational Codeforces Round 59 (Rated for Div. 2)