codechef代码中的错误答案

时间:2014-03-09 06:03:56

标签: c++ data-structures dynamic-programming

我怀疑以下problem。这是answer。当我以这种方式计算方式的数量时,我正在重复,即;例如8可以写成2 + 3 + 3,3 + 5,8 + 0,2 + 2 + 2 + 2.我的代码计算2 + 3 + 3两次。我该如何消除它?谁能告诉我怎么做?

#include<stdio.h>
#include<iostream>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<utility>
#define PB push_back
#define MP make_pair
#define LL long long int
#define M 1000000007
using namespace std;

LL ways[100001][30];
int fib[25]={1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393};

void solve()
{
    int i,j,d,k,f;
    for(j=0;j<30;j++)
    {
        ways[0][j] = 0;
        ways[1][j] = 1;
    }
    for(i=0;i<24;i++)
    {
        for(j=0;j<25;j++)
        {
            if(fib[i] != fib[j])
            ways[fib[i]][j]=1;
        }
    }

    ways[1][0] = 0;
    for(f=0;f<25;f++)
    {
        k = fib[f];
        for(i=2;i<=100000;i++)
        {
            //if(binary_search(fib,fib+26,i))
            {
                for(j=0;j<25;j++)
                {

                    if(fib[j] > i/2)
                    break;
                    if(fib[j] == k )
                    continue;
                    ways[i][f] = ways[i][f] + ways[i-fib[j]][f];
                    //if(i == fib[j])
                    //ways[i][f]++;
                    if(i == 8)
                    cout<<i<<" "<<f<<" "<<fib[j]<<" "<<ways[i][f]<<endl;

                    //cout<<i<<" "<<k<<" "<<fib[j]<<" "<<ways[i][f]<<endl;
                    //system("pause");
                    ways[i][f] %= M;

                }
            }
        }
    }
}


int main()
{
    int t,i,j,k,n;
    solve();
    cin>>t;
    while(t--)
    {
        cin>>n>>k;
        for(i=0;i<25;i++)
        {
            if(fib[i] == k)
            {
                cout<<ways[n][i]<<endl;
                break;
            }
        }
        //cout<<ways[n][k]<<endl;
    }
    return 0;
}

0 个答案:

没有答案