码迷,mamicode.com
首页 > 其他好文 > 详细

2018 焦作网络赛 K Transport Ship ( 二进制优化 01 背包 )

时间:2018-09-19 21:49:53      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:txt   isp   inpu   百度   mil   stdout   bsp   ons   target   

题目链接

题意 : 给出若干个物品的数量和单个的重量、问你能不能刚好组成总重 S

 

分析 :

由于物品过多、想到二进制优化

其实这篇博客就是存个二进制优化的写法

关于二进制优化的详情、百度一下有更多资料

 

技术分享图片
#include<bits/stdc++.h>
#define LL long long
#define ULL unsigned long long

#define scl(i) scanf("%lld", &i)
#define scll(i, j) scanf("%lld %lld", &i, &j)
#define sclll(i, j, k) scanf("%lld %lld %lld", &i, &j, &k)
#define scllll(i, j, k, l) scanf("%lld %lld %lld %lld", &i, &j, &k, &l)

#define scs(i) scanf("%s", i)
#define sci(i) scanf("%d", &i)
#define scd(i) scanf("%lf", &i)
#define scIl(i) scanf("%I64d", &i)
#define scii(i, j) scanf("%d %d", &i, &j)
#define scdd(i, j) scanf("%lf %lf", &i, &j)
#define scIll(i, j) scanf("%I64d %I64d", &i, &j)
#define sciii(i, j, k) scanf("%d %d %d", &i, &j, &k)
#define scddd(i, j, k) scanf("%lf %lf %lf", &i, &j, &k)
#define scIlll(i, j, k) scanf("%I64d %I64d %I64d", &i, &j, &k)
#define sciiii(i, j, k, l) scanf("%d %d %d %d", &i, &j, &k, &l)
#define scdddd(i, j, k, l) scanf("%lf %lf %lf %lf", &i, &j, &k, &l)
#define scIllll(i, j, k, l) scanf("%I64d %I64d %I64d %I64d", &i, &j, &k, &l)

#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define lowbit(i) (i & (-i))
#define mem(i, j) memset(i, j, sizeof(i))

#define fir first
#define sec second
#define VI vector<int>
#define ins(i) insert(i)
#define pb(i) push_back(i)
#define pii pair<int, int>
#define VL vector<long long>
#define mk(i, j) make_pair(i, j)
#define all(i) i.begin(), i.end()
#define pll pair<long long, long long>

#define _TIME 0
#define _INPUT 0
#define _OUTPUT 0
clock_t START, END;
void __stTIME();
void __enTIME();
void __IOPUT();
using namespace std;
const int maxn = 1e6 + 10;
const LL mod = 1e9 + 7;

int num = 0;
LL w[maxn];
LL dp[maxn];

int main(void){__stTIME();__IOPUT();

    int nCase;
    sci(nCase);

    while(nCase--){

        num = 0;

        int n, q;
        scii(n, q);

        for(int i=0; i<n; i++){
            int V, C;
            scii(V, C);
            int tot = (1<<C)-1;
            int j;

            for(j=1; j<=tot; j<<=1)
                w[num++] = 1LL * j * (LL)V % mod, tot -= j;

            if(tot > 0){
                LL rem = tot;
                w[num++] = 1LL * rem * (LL)V % mod;
            }
        }

        mem(dp, 0);
        dp[0] = 1;

        for(int i=0; i<num; i++)
            for(int j=(int)1e4; j>=w[i]; j--)
                dp[j] = (dp[j] + dp[j-w[i]]) % mod;

        while(q--){
            int num;
            sci(num);
            printf("%lld\n", dp[num]);
        }
    }





















__enTIME();return 0;}


void __stTIME()
{
    #if _TIME
        START = clock();
    #endif
}

void __enTIME()
{
    #if _TIME
        END = clock();
        cerr<<"execute time = "<<(double)(END-START)/CLOCKS_PER_SEC<<endl;
    #endif
}

void __IOPUT()
{
    #if _INPUT
        freopen("in.txt", "r", stdin);
    #endif
    #if _OUTPUT
        freopen("out.txt", "w", stdout);
    #endif
}
View Code

 

2018 焦作网络赛 K Transport Ship ( 二进制优化 01 背包 )

标签:txt   isp   inpu   百度   mil   stdout   bsp   ons   target   

原文地址:https://www.cnblogs.com/Rubbishes/p/9676688.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!