欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

loj 300 [CTSC2017]吉夫特 【Lucas定理 + 子集dp】

发布时间:2025/3/20 编程问答 30 豆豆
生活随笔 收集整理的这篇文章主要介绍了 loj 300 [CTSC2017]吉夫特 【Lucas定理 + 子集dp】 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

题目链接

loj300

题解

orz litble
膜完题解后,突然有一个简单的想法:
考虑到\(2\)是质数,考虑Lucas定理:
\[{n \choose m} = \prod_{i = 1} {\lfloor \frac{n}{2^{i - 1}} \rfloor \mod 2^i \choose \lfloor \frac{m}{2^{i - 1}} \rfloor \mod 2^i} \pmod 2\]

\[{n \choose m} = \prod_{each.bit.of.n.and.m} {n' \choose m'} \pmod 2\]
如果二进制下有任何一位\(n\)\(0\)\(m\)不为\(0\),那么就会出现\(m' > n'\)的项,结果就为\(0\)
所以结果不为\(0\),当且仅当二进制下\(m\)\(n\)的子集

所以枚举子集dp即可【\(f[i]\)表示以\(A[u] = i\)\(u\)开头的合法子序列个数】
\([1,n]\)枚举子集的复杂度是\(O(3^{log(max\{a_i\})})\)

#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #define LL long long int #define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt) #define REP(i,n) for (int i = 1; i <= (n); i++) #define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts(""); using namespace std; const int maxn = 250000,maxm = 100005,INF = 1000000000,P = 1e9 + 7; inline int read(){int out = 0,flag = 1; char c = getchar();while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}return out * flag; } int f[maxn],a[maxn],ans,n; int main(){n = read();REP(i,n) a[i] = read();for (int i = n; i; i--){int u = a[i];for (int j = u; j; j = (j - 1) & u){f[u] = (f[u] + f[j]) % P;}ans = (ans + f[u]) % P;f[u]++;}printf("%d\n",ans);return 0; }

转载于:https://www.cnblogs.com/Mychael/p/8987024.html

总结

以上是生活随笔为你收集整理的loj 300 [CTSC2017]吉夫特 【Lucas定理 + 子集dp】的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。