关闭

模板

321人阅读 评论(0) 收藏 举报
//FFT
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;

const int mxn = (1 << 19) + 100;
const double PI = acos(-1.0);
int N = 1 << 19, id = 0, n;
int cnt[mxn];
pair<double,double> P[mxn], PB[mxn], tmp[mxn];

void fill(int s[], int m, int d)
{
    if (m == N)
        P[d] = make_pair(s[id++],0.);
    else 
    {
        fill(s, m<<1, d);
        fill(s, m<<1, d+m);
    }
}

void fill2(int m, int d)
{
    if (m == N) P[d] = tmp[id++];
    else {
        fill2(m<<1, d);
        fill2(m<<1, d+m);
    }
}

inline void FFT(double oper)
{
    for (int d = 0;(1 << d) < N;++d) {
        int m = (1 << d);
        double p0 = PI / m * oper;
        double sinp0 = sin(p0);
        double cosp0 = cos(p0);
        for (int i = 0;i < N;i += (m << 1)) {
            double sinp = 0;
            double cosp = 1;
            for (int j = 0;j < m;++j) {
                double ta = cosp * P[i+j+m].first - sinp*P[i+j+m].second;
                double tb = cosp * P[i+j+m].second + sinp * P[i+j+m].first;
                P[i+j+m].first = P[i+j].first - ta;
                P[i+j+m].second = P[i+j].second - tb;
                P[i+j].first += ta;
                P[i+j].second += tb;
                double tsinp = sinp;
                sinp = sinp * cosp0 + cosp * sinp0;
                cosp = cosp * cosp0 -tsinp * sinp0;
            }
        }
    }
}

void Mul(int p1[],int p2[])
{
    id = 0;
    fill(p1, 1, 0);
    FFT(1.0);
    for (int i = 0;i < N;++i)
        PB[i] = P[i];
    
	id = 0;
    fill(p2, 1, 0);
    FFT(1.0);

    for (int i = 0;i < N;++i)
    {
        tmp[i].first = P[i].first * PB[i].first - P[i].second * PB[i].second;
        tmp[i].second = P[i].first * PB[i].second + P[i].second * PB[i].first;
    }
    id = 0;
    fill2(1, 0);
    FFT(-1.0);
    for (int i = 0;i < N;++i)
    cnt[i] = P[i].first / N + 0.1;
}

int a[250010],b[250010];

int p1[mxn],p2[mxn],ans[mxn];

void solve(int n,int m)
{
	N=n+m;
	while(N!=(N&-N))	N+=(N&-N);

	for(int i=0;i<N;++i)	p1[i]=p2[i]=0;
	for(int i=0;i<n;++i)	p1[i]=(a[i]==0);
	for(int i=1;i<=m;++i)	p2[i-1]=(b[m-i]==0);
	Mul(p1,p2);
	for(int i=m;i<=n;++i)	ans[i-m]=cnt[i-1];

	for(int i=0;i<N;++i)	p1[i]=p2[i]=0;
	for(int i=0;i<n;++i)	p1[i]=(a[i]==1);
	for(int i=1;i<=m;++i)	p2[i-1]=(b[m-i]==1);
	Mul(p1,p2);
	for(int i=m;i<=n;++i)	ans[i-m]+=cnt[i-1];
}

int nxt[250010];

vector<int>V;

void init(int n,int m)
{
	int i,j;
	for(i=1,nxt[0]=j=-1;i<m;i++)
	{
		while(j+1&&b[i]/2-b[j+1]/2)
			j=nxt[j];
		if(b[i]/2==b[j+1]/2)
			j++;
		nxt[i]=j;
	}
	V.clear();
	for(i=0,j=-1;i<n;i++)
	{
		while(j+1&&a[i]/2-b[j+1]/2)
			j=nxt[j];
		if(a[i]/2==b[j+1]/2)
			j++;
		if(j==m-1)
			V.push_back(i-m+1);
	}
}
int getnum(char *s)
{
	int ans=0;
	for(int i=0;s[i];i++)
		ans=ans*2+s[i]-'0';
	return ans;
}
int main()
{
	int n,m,i,j,k;
	char s[20];

	scanf("%d%d",&n,&m);
	for(i=0;i<n;i++)
		scanf("%s",s),a[i]=getnum(s);
	for(i=0;i<m;i++)
		scanf("%s",s),b[i]=getnum(s);

	init(n,m);

	for(i=0;i<n;i++)
		a[i]%=2;
	for(i=0;i<m;i++)
		b[i]%=2;
	if(V.size()==0)
		puts("No");
	else
	{
		solve(n,m);
		puts("Yes");
		int res=-1,pos=-1;
		for(int i=0;i<V.size();++i)
		{
			if(ans[V[i]]<=res)	continue;
			pos=V[i];
			res=ans[pos];
		}
		printf("%d %d\n",m-res,pos+1);
	}
	return 0;
	//a,b里面存现在的数,0或者1,长度分别位n,m,下标从0开始,vector里面存匹配的开始下表
}
//C( n, m ) % p;
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;

#define N 100010

long long mod_pow(int a,int n,int p)
{
    long long ret=1;
    long long A=a;
    while(n)
    {
        if (n & 1)
            ret=(ret*A)%p;
        A=(A*A)%p;
        n>>=1;
    }
    return ret;
}

long long factorial[N];

void init(long long p)
{
    factorial[0] = 1;
    for(int i = 1;i <= p;i++)
        factorial[i] = factorial[i-1]*i%p;
    //for(int i = 0;i < p;i++)
        //ni[i] = mod_pow(factorial[i],p-2,p);
}

long long Lucas(long long a,long long k,long long p) //求C(n,m)%p p最大为10^5。a,b可以很大!
{
    long long re = 1;
    while(a && k)
    {
        long long aa = a%p;long long bb = k%p;
        if(aa < bb) return 0; //这个是最后的改动!
        re = re*factorial[aa]*mod_pow(factorial[bb]*factorial[aa-bb]%p,p-2,p)%p;//这儿的求逆不可先处理
        a /= p;
        k /= p;
    }
    return re;
}

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        long long n,m,p;
        cin >> n >> m >> p;
        init(p);
        cout << Lucas(n+m,m,p) << "\n";
    }
    return 0;
}

//插头dp
#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<queue>
#include<cstring>
#include<map>
#include<cmath>
#include<algorithm>
using namespace std;
#define  ll long long
#define inf 0x3fffffff
#define N 13
#define MOD 60007
#define STA 1000010
  
int n,m,S;
struct HashMap
{
    int hd[MOD],nxt[STA],sta[STA],f[STA],size;
    void init()
    {
        size = 0;
        memset(hd,-1,sizeof(hd));
    }
    void add(int s,int a)
    {
        int h = s%MOD;
        for(int i=hd[h];i+1;i=nxt[i])
        {
            if(sta[i]==s)
            {
                f[i] = min(f[i],a);
                return;
            }
        }
        nxt[size] = hd[h];
        sta[size] = s;
        f[size] = a;
        hd[h] = size++;
        return;
    }
}hs[2];
  
int mmap[N][N];
int solve()
{
    S = (1<<((m+1)<<1))-1;
    int cur = 0;
    hs[0].init();
    hs[0].add(0,0);
      
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++ )
        {
            hs[cur=!cur].init();
            for(int k=0;k<hs[cur^1].size;k++)
            {
                int s = hs[cur^1].sta[k];
                int fk = hs[cur^1].f[k]+1;
                int x = (3<<(j<<1))&s;
                int y = (3<<((j+1)<<1))&s;
                int left = x>>(j<<1);
                int up = y>>((j+1)<<1);
                if(mmap[i][j]==1)
                {
                    if(left==0&&up==0)
                    {
                        hs[cur].add(s,fk-1);
                        if(mmap[i+1][j]&&mmap[i][j+1]) 
                        {
                            hs[cur].add(s|(2<<(j<<1))|(2<<((j+1)<<1)),fk);
                            hs[cur].add(s|(3<<(j<<1))|(3<<((j+1)<<1)),fk);
                        }
                    }
                    else if(left==up&&left)
                    {
                        hs[cur].add(s^x^y,fk);
                    }
                    else if((left==0&&up==2)||(left==2&&up==0))
                    {
                        if(mmap[i+1][j]) hs[cur].add(s^y|(2<<(j<<1)),fk);
                        if(mmap[i][j+1]) hs[cur].add(s^x|(2<<((j+1)<<1)),fk);
  
                    }
                    else if((left==0&&up==3)||(left==3&&up==0))
                    {
                        if(mmap[i+1][j]) hs[cur].add(s^y|(3<<(j<<1)),fk);
                        if(mmap[i][j+1]) hs[cur].add(s^x|(3<<((j+1)<<1)),fk);
  
                    }
                }
                else if(mmap[i][j]==2)
                {
                    if(left==2&&up==0)
                        hs[cur].add(s^x,fk);
                    else if(left==0&&up==2)
                        hs[cur].add(s^y,fk);
                    else if(left==0&&up==0)
                    {
                        if(mmap[i+1][j]==1||mmap[i+1][j]==2) hs[cur].add(s|(2<<(j<<1)),fk);
                        if(mmap[i][j+1]==1||mmap[i][j+1]==2) hs[cur].add(s|(2<<((j+1)<<1)),fk);
                    }
  
                }
                else if(mmap[i][j]==3)
                {
                    if(left==3&&up==0)
                        hs[cur].add(s^x,fk);
                    else if(left==0&&up==3)
                        hs[cur].add(s^y,fk);
                    else if(left==0&&up==0)
                    {
                        if(mmap[i+1][j]==1||mmap[i+1][j]==3) hs[cur].add(s|(3<<(j<<1)),fk);
                        if(mmap[i][j+1]==1||mmap[i][j+1]==3) hs[cur].add(s|(3<<((j+1)<<1)),fk);
                    }
                }
                else
                {
                    if(left==0&&up==0) hs[cur].add(s,fk-1);
                }
  
            }
        }
        hs[cur^1].init();
        for(int i=0;i<hs[cur].size;i++)
            hs[cur^1].add((hs[cur].sta[i]<<2)&S,hs[cur].f[i]);
        cur ^= 1;
          
  
    }
    cur ^= 1;
    for(int i=0;i<hs[cur].size;i++)
        if(hs[cur].sta[i]==0) return hs[cur].f[i]-2;
    return 0;
}
  
int main()
{
      
    int ans;
    while(scanf("%d%d",&n,&m)!=EOF&&(m||n))
    {
          
        memset(mmap,0,sizeof(mmap));
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                scanf("%d",&mmap[i][j]);
                if(mmap[i][j]<2) mmap[i][j] ^= 1;
            }
  
        ans = solve();
        printf("%d\n",ans);
    }
}




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:22400次
    • 积分:499
    • 等级:
    • 排名:千里之外
    • 原创:27篇
    • 转载:0篇
    • 译文:0篇
    • 评论:7条
    最新评论