Codeforces Round #701 (Div. 2) C. Floor and Mod 整除分块

目录

    • 题意
    • 分析
    • Code

题意

给一个数对(n, m),求出在a∈[1,n],b∈[1,m]中满足 ⌊ \lfloor a b \frac{a}{b} ba ⌋ \rfloor = a mod b的数对有多少个

分析

观察样例发现当a=b+1时肯定满足,然后接着往下推

当b=2时
a / b = 3 / 2 … 再往后 ⌊ \lfloor a b \frac{a}{b} ba ⌋ \rfloor 已经大于2了,因此不可能满足

当b=3时
a / b = 4 / 3, 8 / 3

当b=4时
a / b = 5 / 4, 10 / 4, 15 / 4

很容易发现当b = m时,最多只有m-1个a与之对应

然后观察a,发现每个a之间是相差了b+1,计算个数就可以写成 ⌊ \lfloor n b + 1 \frac{n}{b+1} b+1n ⌋ \rfloor

所以可以考虑去枚举b,但一个个去枚举肯定超时,所以想到整除分块

写成式子就是 ∑ i = 2 b m i n ( i − 1 , n / ( i + 1 ) ) \sum_{i=2}^b min(i-1, n /( i+1)) i=2bmin(i1,n/(i+1))其中b为min(n-1, m)

Code

#include 
using namespace std;
//#define ACM_LOCAL
#define fi first
#define se second
#define il inline
#define re register
const int N = 5e5 + 10;
const int M = 5e5 + 10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-5;
const int MOD = 10007;
typedef long long ll;
typedef pair<int, int> PII;
typedef unsigned long long ull;

ll calc(ll n, ll m) {
     
    ll ans = 0;
    for (ll l = 2, r; l <= min(n-1, m); l = r + 1) {
     
        r = min(min(n-1, m), n / (n / (l+1)) - 1);//注意这里的-1
        ans = ans + min(r*(r-1)/2 - (l-1)*(l-2)/2, (r - l + 1) * (n / (l+1)));
    }
    return ans;
}

void solve() {
     
    int T; cin >> T; while (T--) {
     
        ll n, m; cin >> n >> m;
        if (n <= 2 || m < 2) cout << 0 << endl;
        else {
     
            cout << calc(n, m) << endl;
        }
    }
}

signed main() {
     
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifdef ACM_LOCAL
    freopen("input", "r", stdin);
    freopen("output", "w", stdout);
#endif
    solve();
}



你可能感兴趣的:(数论,思维)