关于取模
  • 板块学术版
  • 楼主Error_Eric
  • 当前回复6
  • 已保存回复6
  • 发布时间2021/2/12 12:48
  • 上次更新2023/11/5 03:21:25
查看原帖
关于取模
217300
Error_Eric楼主2021/2/12 12:48

这里有一份代码,小数据过了22个点,估计是没什么算法上的问题了。但是答案很大(因为是组合数学的题目) 题目里要求了对 109+710^9+7 取模,结果我取模炸了。

求助。

#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
#define rei register int
#define heng 1
#define shud 2
#define both 3
int n,m,a[255][255],p[255],q[255],r[255],d[255];
char c[1005][1005];
const int modd=1e9+7;
int anal(int fx,int fy){
    int cnt=0;
    for(rei i=0;i<3;i++)
        for(rei j=0;j<3;j++)
            cnt+=(c[fx+i][fy+j]=='O');
    switch(cnt){
        case 1:return both;break;
        case 4:return both;break;
        case 5:return both;break;
        case 2:
            if(c[fx][fy]=='O')return heng;
            else return shud;
            break;
        case 3:
            if(c[fx][fy]=='O')return heng;
            else return shud;
            break;
        case 6:
            if(c[fx][fy+1]=='O')return heng;
            else return shud;
            break;
        default:return both;
    }
}
int main(){
    scanf("%d%d",&n,&m);
    if(n*m&1)return printf("%d\n",0),0;
    for(rei i=1;i<=n*4+1;i++)
        for(rei j=1;j<=m*4+1;j++)
            scanf(" %c",&c[i][j]);
    for(rei i=1;i<=n;i++)
        for(rei j=1;j<=m;j++)
            a[i][j]=anal(i*4-2,j*4-2);
    for(rei j=1;j<=m;j++){
        p[j]=((n&1)==0);
        for(rei i=1;(i<=n) and (p[j]==1);i++)
            if(a[i][j]==heng)p[j]=0;
    }
    for(rei j=2;j<=m;j++){
        for(rei i=1;i<=n;i++)r[i]=0;r[0]=1;
        for(rei i=1;i<=n;i++){
            if(a[i][j]!=shud and a[i][j-1]!=shud )r[i]+=r[i-1];
            if(a[i][j]!=heng and a[i][j-1]!=heng and a[i-1][j]!=heng and a[i-1][j-1]!=heng and i>1)r[i]+=r[i-2];
        }
        q[j]=r[n]%modd;
    }
    d[0]=1,q[0]=1;
    for(rei j=1;j<=m;j++){
        d[j]=d[j-1]*p[j]%modd;
        if(j>1)d[j]+=(d[j-2]*q[j]-d[j-2]*p[j-1]*p[j])%modd,d[j]%=modd;
    }
    printf("%d\n",d[m]%modd);
    return 0;
}

link

2021/2/12 12:48
加载中...