poj 3150 Cellular Automaton 夜

http://poj.org/problem?id=3150

不擅长数学类的题呀

网上很多此题的解题报告 都不错 但是没有注释

推荐解题报告:http://www.cppblog.com/varg-vikernes/archive/2011/02/08/139804.html

我的代码及其注释:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#define LL long long

using namespace std;

const int N=505;
LL a[N];
LL b[N];
LL temp[N];
int n,m,d,k;
void Multi(LL l1[],LL l2[])//把 l1*l2 赋值给 l1
{
    for(int i=0;i<n;++i)
    {
        temp[(n-i)%n]=0;
        for(int j=0;j<n;++j)
        {
            temp[(n-i)%n]+=(l2[(i+j)%n]*l1[j]);
        }
    }
    for(int i=0;i<n;++i)
    l1[i]=temp[i];
}
int main()
{
    //freopen("data.txt","r",stdin);
    while(scanf("%d %d %d %d",&n,&m,&d,&k)!=EOF)
    {
        for(int i=0;i<n;++i)
        scanf("%I64d",&a[i]);
        memset(b,0,sizeof(b));
        for(int i=0,j=0;i<=d;++i,--j)
        b[i]=b[j+n]=1;
        while(k)
        {
            if(k&1)//如果这时的k末位为1
            Multi(a,b);//则正好乘上这时的b
            Multi(b,b);//b每次循环都变成了原来的b*b
            k=k>>1;
        }
        for(int i=0;i<n;++i)
        {
            if(i)
            printf(" ");
            printf("%I64d",a[i]%m);
        }
        printf("\n");
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/liulangye/p/2613538.html