bzoj4589: Hard Nim fwt

题意:求n个m以内的素数亦或起来为0的方案数
题解:fwt板子题,先预处理素数,把m以内素数加一遍(下标),然后fwt之后快速幂即可,在ifwt之后a【0】就是答案了

/**************************************************************
    Problem: 4589
    User: walfy
    Language: C++
    Result: Accepted
    Time:4984 ms
    Memory:1928 kb
****************************************************************/
 
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
//#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
template<typename T>
inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>
inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
 
using namespace std;
 
const double eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=50000+10,maxn=400000+10,inf=0x3f3f3f3f;
 
int prime[N],cnt,a[N*2];
bool mark[N];
int inv=qp(2,mod-2);
void init()
{
    for(int i=2;i<N;i++)
    {
        if(!mark[i])prime[++cnt]=i;
        for(int j=1;j<=cnt&&i*prime[j]<N;j++)
        {
            mark[i*prime[j]]=1;
            if(i%prime[j]==0)break;
        }
    }
}
void fwt(int *a,int n,int dft)
{
    for(int i=1;i<n;i<<=1)
    {
        for(int j=0;j<n;j+=(i<<1))
        {
            for(int k=j;k<j+i;k++)
            {
                int x=a[k],y=a[k+i];
                a[k]=(x+y)%mod;a[k+i]=(x-y+mod)%mod;
                if(dft==-1)a[k]=1ll*a[k]*inv%mod,a[k+i]=1ll*a[k+i]*inv%mod;
            }
        }
    }
}
int main()
{
    init();
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        memset(a,0,sizeof a);
        for(int i=1;i<=cnt&&prime[i]<=m;i++)a[prime[i]]=1;
        int len=1;while(len<=m)len<<=1;
        fwt(a,len,1);
        for(int i=0;i<=len;i++)a[i]=qp(a[i],n);
        fwt(a,len,-1);
        printf("%d\n",a[0]);
    }
    return 0;
}
/********************
 
********************/
posted @ 2018-08-21 13:41  walfy  阅读(131)  评论(0编辑  收藏