这是题解
【B】花与剑的轮舞
大体思路
首先找出对于特定的
具体思路
首先,我们会有一份相对没那么暴力的暴力程序,如下:
#include<bits/stdc++.h>
using namespace std;
#define int long long
constexpr int mod = 1e9 + 9;
inline void mad(int& l, const int& r) { (l += r % mod) >= mod && (l -= mod); }
int n, m, v[5005][5005], ans[4];
inline int get(int lt, int ch) {
if (v[lt][ch]) return v[lt][ch]; if (lt < ch) return 0;
return v[lt][ch] = lt / ch + get(lt % ch + lt / ch, ch);
}
signed main() {
cin >> n >> m;
for (int i = 1; i <= n; ++i)
for (int j = 2; j <= m; ++j)
mad(ans[0], i + get(i, j));
cout << ans[0] << endl;
}
也就是加了个记忆化。 事实上,以上的程序在本题
首先给出结论:若有
首先,抛出第一个子问题:如果允许借一瓶水,那么最多能喝到多少瓶?答案是
首先,你能喝到最开始的那
随后,
接下来抛出第二个子问题的结论:当且仅当
首先,这一瓶实际上差在不能借水这一点上。显然,对于
然后,我们考虑当
我们仔细观察第二个结论,不难发现他可以变为
#include<bits/stdc++.h>
using namespace std;
#define int long long
constexpr int mod = 1e9 + 9; int n,m,ans[4];
inline void mad(int& l, const int& r) { (l += r % mod) >= mod && (l -= mod); }
signed main() {
cin >> n >> m;
for (int i = 1; i <= n; ++i)
for (int j = 2; j <= m; ++j)
mad(ans[1], i + (i - 1) / (j - 1));
cout << ans[1] << endl;
return 0;
}
看到
我们令
这时候我们就可以得到一个
误导性其实也有,你可以在推式子时仍旧选择以
这时候,你有
#include<bits/stdc++.h>
using namespace std;
#ifndef _MSC_VER
#define int __int128
#endif
constexpr int mod = 1e9 + 9;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#define putchar_unlocked _putchar_nolock
#endif
inline int read() {
int r = 0; char c = getchar_unlocked();
while (c < '0' || c>'9') c = getchar_unlocked();
while (c >= '0' && c <= '9') r = r * 10 + (c ^ 48), c = getchar_unlocked();
return r;
}
inline void write(int x) {
if (x > 9) write(x / 10);
putchar_unlocked(x % 10 ^ 48);
return;
}
inline void writi(int args) {
write(args); putchar_unlocked(10);
}
inline void mad(int& l, const int& r) { (l += r % mod) >= mod && (l -= mod); }
int n, m, ans[4];
signed main() {
n = read(), m = read();
ans[2] = n % mod * ((n + 1) % mod) / 2 % mod * ((m - 1) % mod) % mod;
for (int j = 1; j < m; ++j) {
int mp = (n - 1) / j % mod;
int bas = mp * (mp - 1) / 2 % mod * j;
mad(ans[2], bas);
int adc = (n - j * mp % mod) % mod * mp % mod;
mad(ans[2], adc);
}
//two step, O(n)
writi(ans[2]);
return 0;
}
//私は猫です
我们继续优化一下:以
我们设
至此,此题复杂度优化至
#include<bits/stdc++.h>
using namespace std;
#ifndef _MSC_VER
#define int __int128
#endif
constexpr signed mod = 1e9 + 9;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#define putchar_unlocked _putchar_nolock
#endif
inline int read() {
int r = 0; char c = getchar_unlocked();
while (c < '0' || c>'9') c = getchar_unlocked();
while (c >= '0' && c <= '9') r = r * 10 + (c ^ 48), c = getchar_unlocked();
return r;
}
inline void write(int x) {
if (x > 9) write(x / 10);
putchar_unlocked(x % 10 ^ 48);
}
inline void writi(int args) {
write(args); putchar_unlocked(10);
}
inline void mad(int& l, const int& r) { (l += r % mod) >= mod && (l -= mod); }
int n, m, ans;
signed main() {
n = read(), m = read();
ans = n * (n + 1) / 2 % mod * (m - 1) % mod;
n--; m--;
for (int l = 1, r = 0; l <= n && l <= m; l = r + 1) {
r = min(n / (n / l), m); int vl = n / l % mod;
int t = (vl * (vl - 1) >> 1) % mod;
int s = ((r + l) * (r - l + 1) >> 1) % mod;
mad(ans, t * s % mod);
int cl = (n + 1) * (r - l + 1) * vl % mod;
mad(ans, cl);
int cr = vl * vl * s % mod;
mad(ans, mod - cr);
}
writi(ans);
return 0;
}
//私は猫です