目錄
- 題意
- 分析
- 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=2b?min(i?1,n/(i+1))其中b為min(n-1, m)
Code
#include <bits/stdc++.h>
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();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/259508.html
標籤:其他
上一篇:codeforces1485 E. Move and Swap(dp)
下一篇:Android——猜數字小游戲
