poj 3601 hanoi 变形

每个盘独一无二,大小为i个盘有s[i]个,求最小移动步数,移动后和原序列相同


#include 
#define N 101

int main()
{
    int a[N] = {0}, ///最下面一种盘 反向
        b[N] = {0}, ///最下面一种盘 正向
        s[N],m,n;

    while(scanf("%d%d",&n,&m) != EOF)
    {
        int i,j;
        for(i = 1; i <= n; ++i)
            scanf("%d",&s[i]);
        a[1] = s[1] % m;
        b[1] = (2 * s[1] - 1) % m;
        for(i = 2; i <= n; ++i)
        {
            a[i] = ( (a[i-1] << 1) + s[i]) % m;
            if(s[i] == 1)
                b[i] = a[i];
            else
                b[i] = (b[i-1] + (a[i-1]<<1) + (s[i]<<1)) % m;///a方法移两次便正向了
        }
        printf("%d\n",b[n]);
    }
    return 0;
}


你可能感兴趣的:(Data,Struct)