usaco 6.1 Postal Vans(插头DP)

Postal Vans

ACM South Pacific Region -- 2003

Tiring of their idyllic fields, the cows have moved to a new suburb. The suburb is a rectangular grid of streets with a post office at its Northwest corner. It has four avenues running East-West and N (1 <= N <= 1000) streets running North-South.

For example, the following diagram shows such a suburb with N=5 streets, with the avenues depicted as horizontal lines, and the post office as a dark blob at the top-left corner:

Each day the postal van leaves the post office, drives around the suburb and returns to the post office, passing exactly once through every intersection (including those on borders or corners). The executives from the post company want to know how many distinct routes can be established for the postal van (of course, the route direction is significant in this count).

For example, the following diagrams show two such routes for the above suburb:

As another example, the following diagrams show all the four possible routes for a suburb with N=3 streets:

Write a program that will determine the number of such distinct routes given the number of streets.

PROGRAM NAME: vans

INPUT FORMAT

  • Line 1: A single integer, N

SAMPLE INPUT (file vans.in)

4

OUTPUT FORMAT

  • Line 1: A single integer that tells how many possible distinct routes corresponding to the number of streets given in the input.

SAMPLE OUTPUT (file vans.out)

12
题意:求4行n列的一条回路问题,回路分方向。。。
分析:一条回路问题,而且只要4行,直接暴力枚举两列之间所有状态转移,然后写个大数就搞定了
PS:好久没写大数,wa了一大堆啊T_T
代码:
/*
ID: 15114582
PROG: vans
LANG: C++
*/
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
struct BigNum
{
    int s[111],w;
    BigNum()
    {
        w=1;
        memset(s,0,sizeof(s));
    }
    void add(BigNum a)
    {
        if(a.w>w)w=a.w;
        for(int i=0;i<w;++i)
        {
            s[i]+=a.s[i];
            s[i+1]+=s[i]/100000;
            s[i]%=100000;
        }
        while(s[w])++w;
    }
    void out()
    {
        printf("%d",s[w-1]);
        for(int i=w-2;i>=0;--i)
            printf("%05d",s[i]);
        puts("");
    }
}f[1111][6],ans;
int i,j,n;
int main()
{
    freopen("vans.in","r",stdin);
    freopen("vans.out","w",stdout);
    f[1][0].s[0]=f[1][1].s[0]=1;
    for(i=2;i<1001;++i)
    {
        f[i][0].add(f[i-1][0]);
        f[i][0].add(f[i-1][2]);
        f[i][0].add(f[i-1][4]);
        f[i][1]=f[i][0];
        f[i][1].add(f[i-1][3]);
        f[i][2].add(f[i-1][1]);
        f[i][2].add(f[i-1][5]);
        f[i][3]=f[i-1][1];
        f[i][4]=f[i][2];
        f[i][5]=f[i][2];
    }
    while(~scanf("%d",&n))
    {
        ans=f[n-1][1];
        ans.add(f[n-1][5]);
        ans.add(ans);
        ans.out();
    }
	return 0;
}


你可能感兴趣的:(C++,File,Integer,Office,input,output)