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0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 1
然后我太菜了,就到此为止了。
看了下题解的做法一,发现很巧妙,因为我们求的是对取模后的答案,所以我们将题目里的多项式配上一个对答案不造成影响!
由二项式定理可知:
对于展开式中含有项的为,系数为。
代入得,对于和的奇偶性有关,
- 是奇数
- 是偶数
Code_bruceforce
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr ll N = 1e6 + 5; int f[100][N]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); f[0][0] = 1; cout << setw(2) << 0 << " : " << 1 << "\n"; for (int i = 1; i <= 30; i++) { cout << setw(2) << i << " : "; for (int j = 0; j <= 2 * i; j++) { if (j - 2 >= 0) f[i][j] += f[i - 1][j - 2]; if (j - 1 >= 0) f[i][j] += f[i - 1][j - 1]; f[i][j] += f[i - 1][j]; f[i][j] %= 3; cout << f[i][j] << " \n"[j == 2 * i]; } } return 0; }
Code
#include <bits/stdc++.h> #define fi first #define se second #define mp make_pair #define lowbit(x) ((x) & -(x)) using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; constexpr double eps = 1e-8; constexpr int NINF = 0xc0c0c0c0; constexpr int INF = 0x3f3f3f3f; constexpr ll mod = 3; constexpr ll N = 1e6 + 5; /* (x ^ 2 + x + 1) ^ n = (x ^ 2 - 2 * x + 1) ^ n = (x - 1) ^ (2 * n) ans = C(2 * n, k) x ^ k * (-1) ^ (2 * n - k) % 3 */ ll p = 3; inline ll qpow(ll a, ll b) { ll base = a % p; ll ans = 1; while (b > 0) { if (b & 1) ans = (ans * base) % p; base = base * base % p; b >>= 1; } return ans; } inline ll C(ll n, ll m) { if (n < m) return 0;//组合数n<m特判 if (m > n - m) m = n - m;//组合数性质 ll a = 1, b = 1; for (int i = 0; i < m; i++) { a = (a * (n - i) % p);//组合数分子 a b = (b * (i + 1)) % p;//组合数分母 b } return a * qpow(b, p - 2) % p;//费马小定理 a/b=a*b^(p-2) } inline ll Lucas(ll n, ll m) { if (m == 0) return 1; return Lucas(n / p, m / p) * C(n % p, m % p) % p; } void solve() { ll n, k; cin >> n >> k; n = 2 * n; cout << (Lucas(n, k) * ((n - k) & 1 ? -1 : 1) % mod + mod) % mod << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { solve(); } return 0; }