状态压缩dp优化一半指数的方法
作者:互联网
状态压缩dp的时间复杂度一般时O(n*2^m)
可以将2^m优化为2^(m/2)
具体看例题:
牛客网小白月赛53 F. Freezing
https://ac.nowcoder.com/acm/contest/11230/F
dp[hi][li]数组的状态定义为,高8位为hi,低8位与li没有交集
这样就可以用两个2^(m/2)的操作更新低i个数。
第i个数计算出的tmp,即为以ai结尾的方法个数
#include<bits/stdc++.h> using namespace std; typedef long long LL; int MOD = 998244353; LL dp[1 << 8][1<<8]; LL nums[200010]; void YD() { int n, m; cin >> n >> m; //nxt[0].push_back(0); char str[17]; for (int i = 1; i <= n; i++) { cin >> str; for (int j = 0; j < m; j++) { if (str[j] == 'o') nums[i] += (1 << j); } } LL res = 0; for (int i = 1; i <= n; i++) { LL num = nums[i]; LL tmp = 1; int hi = (num >> 8); int low = num^(hi<<8); for (int i = 0; i < (1 << 8); i++) { if ((i & hi) == 0) tmp += dp[i][low]; tmp %= MOD; } res += tmp; res %= MOD; for (int i = 0; i < (1 << 8); i++) { if ((i & low) == 0) dp[hi][i]= (dp[hi][i]+tmp)%MOD; } } cout << res << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int T = 1; //cin >> T; while (T--) { YD(); } return 0; }View Code
https://ac.nowcoder.com/discuss/982292?type=101&order=0&pos=1&page=1&channel=-1&source_id=1
标签:ac,int,压缩,个数,hi,str,优化,dp 来源: https://www.cnblogs.com/ydUESTC/p/16468894.html