zoukankan      html  css  js  c++  java
  • codeforces 388D Fox and Perfect Sets(线性基+数位dp)

    #include<bits/stdc++.h>
    using namespace std;
    #define fi first
    #define se second
    #define mp make_pair
    #define pb push_back
    #define rep(i, a, b) for(int i=(a); i<(b); i++)
    #define sz(x) (int)x.size()
    #define de(x) cout<< #x<<" = "<<x<<endl
    #define dd(x) cout<< #x<<" = "<<x<<" "
    typedef long long ll;
    typedef pair<int, int> pii;
    typedef vector<int> vi;
    
    const int P=1e9+7;
    int n, k;
    int a[33];
    ll f[33][33][2], pw[33];
    
    void upd(ll &a, ll b) {
    	a+=b;
    	if(a>=P) a-=P;
    }
    void init() {
    	pw[0]=1;
    	rep(i,1,33) pw[i]=pw[i-1]*2%P;
    }
    
    int main() {
    	init();
    	while(~scanf("%d",&k)) {
    		n=0;
    		while(k) {
    			a[++n]=(k&1);
    			k>>=1;
    		}
    		for(int l=1, r=n;l<r;++l, --r) swap(a[l], a[r]);
    		memset(f,0,sizeof(f));
    		f[0][0][1]=1;
    		rep(i,0,n) rep(j,0,i+1) {
    			if(f[i][j][0]) {
    				upd(f[i+1][j+1][0], f[i][j][0]);
    				upd(f[i+1][j][0], f[i][j][0]*pw[j]%P);
    			}
    			if(f[i][j][1]) {
    				if(a[i+1]) upd(f[i+1][j+1][1], f[i][j][1]);
    				if(a[i+1]) upd(f[i+1][j][0], f[i][j][1]*(j?pw[j-1]:1)%P);
    				upd(f[i+1][j][1], f[i][j][1]*(j?pw[j-1]:0)%P);
    			}
    		}
    		ll ans=0;
    		rep(i,0,n+1) rep(j,0,2) if(f[n][i][j]) {
    			upd(ans, f[n][i][j]);
    		}
    		printf("%lld
    ",ans);
    	} 
    	return 0;
    }
    
  • 相关阅读:
    HDU 5794
    HDU 5794
    3070 Fibonacci 矩阵快速幂
    数论基础
    hdu 1061 Rightmost Digit 快速幂
    poj 2305 Basic remains java
    poj 1001 Exponentiation
    hdu 2054 A == B ? (java)
    java大数练习
    hdu3018 Ant Trip 欧拉回路
  • 原文地址:https://www.cnblogs.com/wuyuanyuan/p/8598194.html
Copyright © 2011-2022 走看看