sgu 225 分类: sgu 2015-06...


状压DP,并没有加什么优化。。。


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>

const int maxn = 15, sz = 10;
struct Trans
{
    int p1,p2,p3,cnt;
    Trans(){}
    Trans(int p1,int p2,int p3,int cnt):p1(p1),p2(p2),p3(p3),cnt(cnt){}
};

int n, k; int tl; Trans ts[1<<(sz<<1)];
long long f[2][sz*sz+1][1<<sz][1<<sz];
long long ans[sz*sz+1]; int flag;

#define STUS1   p1|(i*(1<<col))
#define STUS2   p2|(j*(1<<col))
#define STUS3   p3|(p*(1<<col))

void DFS0(int col,int p1,int p2,int cnt)
{
    if(col == n)
        f[flag][cnt][p1][p2] = 1;
    else
    {
        int t1 = 0, t2 = 0;

        if(col < 2 || !(p2&(1<<(col-2)))) t1 = 1;
        if(col < 2 || !(p1&(1<<(col-2)))) t2 = 1;

        for(int i = 0; i <= t1; i++)
            for(int j = 0; j <= t2; j++)
                DFS0(col+1, STUS1, STUS2, cnt+i+j);     
    }
}

void DFS(int col,int p1,int p2,int p3,int cnt)
{
    if(col == n)
        ts[++tl] = Trans(p1,p2,p3,cnt);
    else
    {
        int t1 = 0, t2 = 0, t3 = 0;

        if(col < 2 || !(p2&(1<<(col-2))))
            if(col < 1 || !(p3&(1<<(col-1)))) t1 = 1;

        if(col < 2 || !(p1&(1<<(col-2))))
            if(col < 2 || !(p3&(1<<(col-2)))) t2 = 1;

        if(col < 1 || !(p1&(1<<(col-1))))
            if(col < 2 || !(p2&(1<<(col-2)))) t3 = 1;

        for(int i = 0; i <= t1; i++)
            for(int j = 0; j <= t2; j++)
                for(int p = 0; p <= t3; p++)
                    DFS(col+1, STUS1, STUS2, STUS3, cnt+p);     
    }
}
#undef STUS1
#undef STUS2
#undef STUS3                

void DP()
{
    for(int i = 3; i <= n; i++)
    {
        flag ^= 1;

        for(int j = 1; j <= tl; j++)
            for(int p = ts[j].cnt; p <= k; p++) 
                f[flag][p][ts[j].p2][ts[j].p3] += f[flag^1][p-ts[j].cnt][ts[j].p1][ts[j].p2];   

        for(int p = 0; p <= k; p++)
            for(int j = 1; j <= tl; j++)
                f[flag^1][p][ts[j].p1][ts[j].p2] = 0;                   
    }
    for(int p = 0; p <= k; p++)
        for(int j = 0; j < (1<<n); j++)
            for(int i = 0; i < (1<<n); i++)
                ans[p] += f[flag][p][i][j];
}

int main()
{   
#ifndef ONLINE_JUDGE    
    freopen("sgu225.in","r",stdin);
    freopen("sgu225.out","w",stdout);
#endif

    std::cin >> n >> k;

    DFS0(0,0,0,0), DFS(0,0,0,0,0), DP();

    std::cout << ans[k];

//  for(int i = 0; i <= k; i++)
//      std::cout << ans[i] << "LL,";

#ifndef ONLINE_JUDGE    
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}

结果发现MLE了,蛤蛤。


你真的以为上面的代码能过吗?
还是太年轻了。。。


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>

int n, k;

long long list[15][110] = {

{0},    
{1,1},
{1,4,6,4,1},
{1,9,28,36,18,2,0,0,0,0},
{1,16,96,276,412,340,170,48,6,0,0,0,0,0,0,0,0},
{1,25,252,1360,4436,9386,13384,12996,8526,3679,994,158,15,1,0,0,0,0,0,0,0,0,0,0,0,0},
{1,36,550,4752,26133,97580,257318,491140,688946,716804,556274,323476,141969,47684,12488,2560,393,40,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{1,49,1056,13340,111066,649476,2774728,8891854,21773953,41310504,61412928,72304650,68233368,52400729,33376132,18001362,8363763,3374053,1178064,351592,87913,17910,2858,336,26,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{1LL,64LL,1848LL,32084LL,376560LL,3184708LL,20202298LL,98796304LL,379978716LL,1167053680LL,2897726604LL,5876860140LL,9825415557LL,13660238780LL,15932672964LL,15737653004LL,13304668385LL,9742722088LL,6260518246LL,3574590840LL,1830733371LL,844203844LL,349524138LL,128874944LL,41833846LL,11792736LL,2840224LL,572432LL,93840LL,12004LL,1122LL,68LL,2LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL},
{1LL,81LL,3016LL,68796LL,1080942LL,12472084LL,110018552LL,762775440LL,4241252429LL,19206532478LL,71707869632LL,222946143752LL,582155146204LL,1286247689414LL,2421159140764LL,3908273840366LL,5446391581062LL,6599640204257LL,7010436668992LL,6589213734278LL,5537849837497LL,4207779106033LL,2920161348852LL,1865346129716LL,1101125592067LL,600730512987LL,302041066250LL,139345014744LL,58692638521LL,22451454400LL,7755194754LL,2403337080LL,663103709LL,161373907LL,34237130LL,6238414LL,957145LL,120334LL,11914LL,872LL,42LL,1LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL},
{1LL,100LL,4662LL,135040LL,2732909LL,41199404LL,481719518LL,4491423916LL,34075586550LL,213628255072LL,1120204619108LL,4961681221524LL,18715619717199LL,60541371615660LL,168976761361446LL,409191804533576LL,864172675710439LL,1599730843649564LL,2609262108838924LL,3770687313420780LL,4857550050070531LL,5616928666465104LL,5874943705896600LL,5604501518609804LL,4917655076255841LL,3999855946779732LL,3034690618677388LL,2156485957257040LL,1437827591264317LL,899278231344296LL,526753407546620LL,288274613750624LL,146990556682887LL,69626509814580LL,30542906352994LL,12366448408056LL,4604442057431LL,1569983914256LL,487876545370LL,137395261280LL,34831261750LL,7884855000LL,1578162590LL,275861904LL,41455966LL,5246412LL,543534LL,44244LL,2652LL,104LL,2LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL,0LL}
                    };

int main()
{
#ifndef ONLINE_JUDGE    
    freopen("sgu225.in","r",stdin);
    freopen("sgu225.out","w",stdout);
#endif

    std::cin >> n >> k;
    std::cout << list[n][k]; 

#ifndef ONLINE_JUDGE    
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/dashgua/p/4723006.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值