P2396 yyy loves Maths VII 状压dp 变态卡常
生活随笔
收集整理的這篇文章主要介紹了
P2396 yyy loves Maths VII 状压dp 变态卡常
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
很容易想到dp,但是1<<24都有1600w的復雜度,要是普通做法肯定T掉
共有maxs=(1<<n)-1種情況
首先f[ ]記錄某種情況的方案數,dis[ ]記錄某種情況的步數和
然后每次到dis[i]=厄運值就不更新
解決卡常的方法
正常做法:2^(n+1) 肯定會T
改成 :2^nlogn? O2優化過了 惡心心
f[i]=去掉i中某一個1的所有情況f[j]和
尋找某一個1用lowbit
刪除某一個1用異或^
代碼如下:
// luogu-judger-enable-o2//#pragma comment (linker, "/STACK:102400000,102400000")
#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<set>
#include<stack>
#include<vector>
#include<map>
#include<queue>
#include<list>
#include<time.h>
#include<bitset>#define myself i,l,r
#define lson i<<1
#define rson i<<1|1
#define Lson i<<1,l,mid
#define Rson i<<1|1,mid+1,r
#define half (l+r)/2
#define lowbit(x) x&(-x)
#define min4(a, b, c, d) min(min(a,b),min(c,d))
#define min3(x, y, z) min(min(x,y),z)
#define max3(x, y, z) max(max(x,y),z)
#define max4(a, b, c, d) max(max(a,b),max(c,d))
#define pii make_pair
#define pr pair<int,int>
typedef unsigned long long ull;
typedef long long ll;
const int inff = 0x3f3f3f3f;
const long long inFF = 9223372036854775807;
const int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
const int mdir[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, -1, 1, 1, -1, -1, -1};
const double eps = 1e-10;
const double PI = acos(-1.0);
const double E = 2.718281828459;
using namespace std;
const int mod=1e9+7;
const int maxn=1e5+51;
int f[1<<24],dis[1<<24];
int eyun[2];
int n,op;
int main()
{cin>>n;int maxs=(1<<n)-1;for(int i=0;i<n;i++) scanf("%d",&dis[1<<i]);cin>>op;for(int i=0;i<op;i++) scanf("%d",&eyun[i]);f[0]=1;for(int i=1;i<=maxs;i++){int j=i&(-i),k=i;dis[i]=dis[i^j]+dis[j];if(dis[i]==eyun[0]||dis[i]==eyun[1]) continue;while(k){j=k&(-k);f[i]+=f[i^j];if(f[i]>=mod) f[i]-=mod;k^=j;}}printf("%d\n",f[maxs]);return 0;
}
?
總結
以上是生活随笔為你收集整理的P2396 yyy loves Maths VII 状压dp 变态卡常的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: P2114 [NOI2014]起床困难综
- 下一篇: 洛谷P2397 yyy loves Ma