题解 | #马老师# 多重背包
马老师
https://ac.nowcoder.com/acm/problem/222785
题目大意
一个合法的序列是一个长度为并且和为的非负整数序列,并且对于给出的序列还需要满足任意位置,给出询问共有多少个合法的序列,对取模。
Solution
看到运算,就需要往二进制拆分上面靠,那么对于一个他的第位二进制位是,那么从到每一个数的第个二进制位都应该为。
那么题目就变成了你可以用任意的幂次去构造,并且每个的幂次最多使用次,问构造出的方案数。
等价于存在购买上限的多重背包问题,可以使用容斥解决问题,时间复杂度,是可选的二进制幂次数量,是题目给出的,因为所以
#include <bits/stdc++.h> using namespace std; #define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0) #define all(__vv__) (__vv__).begin(), (__vv__).end() #define endl "\n" #define pai pair<int, int> #define ms(__x__,__val__) memset(__x__, __val__, sizeof(__x__)) #define rep(i, sta, en) for(int i=sta; i<=en; ++i) #define repp(i, sta, en) for(int i=sta; i>=en; --i) #define debug(x) cout << #x << ":" << x << '\n' typedef tuple<int, int> tup; typedef long long ll; typedef unsigned long long ull; typedef long double ld; inline ll read() { ll s = 0, w = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') w = -1; for (; isdigit(ch); ch = getchar()) s = (s << 1) + (s << 3) + (ch ^ 48); return s * w; } inline void print(ll x, int op = 10) { if (!x) { putchar('0'); if (op) putchar(op); return; } char F[40]; ll tmp = x > 0 ? x : -x; if (x < 0)putchar('-'); int cnt = 0; while (tmp > 0) { F[cnt++] = tmp % 10 + '0'; tmp /= 10; } while (cnt > 0)putchar(F[--cnt]); if (op) putchar(op); } inline ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } ll qpow(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans *= a; b >>= 1; a *= a; } return ans; } ll qpow(ll a, ll b, ll mod) { ll ans = 1; while (b) { if (b & 1)(ans *= a) %= mod; b >>= 1; (a *= a) %= mod; }return ans % mod; } const int dir[][2] = { {0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1} }; const int MOD = 1e9 + 7; const int INF = 0x3f3f3f3f; const ll INF64 = 0x3f3f3f3f3f3f3f3f; const int N = 5e6 + 7; struct Node { ll val; int id; bool operator < (const Node& opt) const { return val < opt.val; } }; ll n, m; ll f[N]; int w[] = { 0,1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304 }; // 23 int solve() { // for (int i = 1;; i *= 2) { // if (i > 5e6) break; // print(i, ','); // } // cout << endl; n = read(), m = read(); f[0] = 1; for (int i = 1; i <= 23; ++i) { for (int j = w[i]; j <= m; ++j) { f[j] = (f[j] + f[j - w[i]]) % MOD; } for (int j = m; j >= (n + 1) * w[i]; --j) { f[j] = (f[j] - f[j - (n + 1) * w[i]] + MOD) % MOD; } } print(f[m]); return 1; } int main() { //int T = read(); rep(_, 1, T) { solve(); //cout << (solve() ? "YES" : "NO") << endl; } return 0; }
tmp 文章被收录于专栏
堆放杂物------