zoukankan      html  css  js  c++  java
  • Codeforces Gym

    A. Coins
    time limit per test
    3 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Hasan and Bahosain want to buy a new video game, they want to share the expenses. Hasan has a set of N coins and Bahosain has a set of M coins. The video game costs W JDs. Find the number of ways in which they can pay exactly W JDs such that the difference between what each of them payed doesn’t exceed K.

    In other words, find the number of ways in which Hasan can choose a subset of sum S1 and Bahosain can choose a subset of sum S2such that S1 + S2 = W and |S1 - S2| ≤ K.

    Input

    The first line of input contains a single integer T, the number of test cases.

    The first line of each test case contains four integers NMK and W (1 ≤ N, M ≤ 150) (0 ≤ K ≤ W(1 ≤ W ≤ 15000), the number of coins Hasan has, the number of coins Bahosain has, the maximum difference between what each of them will pay, and the cost of the video game, respectively.

    The second line contains N space-separated integers, each integer represents the value of one of Hasan’s coins.

    The third line contains M space-separated integers, representing the values of Bahosain’s coins.

    The values of the coins are between 1 and 100 (inclusive).

    Output

    For each test case, print the number of ways modulo 1e9 + 7 on a single line.

    Example
    input
    2
    4 3 5 18
    2 3 4 1
    10 5 5
    2 1 20 20
    10 30
    50
    output
    2
    0
    题目大意:分别求出两人硬币的部分和s1和s2,使得s1+s2=w,且|s1-s2|<=k,求出选取方法数。
    方法:
    由s1+s2=w,且|s1-s2|<=k得(w-k)/2=<s1<=(w+k)/2;

    分别对两人的硬币选取方法进行01背包,然后遍历求和。
    代码:
    #include<iostream>
    #include<cstdio>
    #include<cstring> 
    using namespace std;
    #define ll long long
    const int mod=1e9+7; 
    const int N=155;
    const int W=15000+5;
    int a[N],b[N];
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            int n,m,k,w;
            ll dp1[W]={
                1
            },dp2[W]={
                1
            };
            scanf("%d %d %d %d",&n,&m,&k,&w);
            for(int i=0;i<n;i++)
            scanf("%d",a+i);
            for(int i=0;i<m;i++)
            scanf("%d",b+i);
            for(int i=0;i<n;i++)
            {
                for(int j=w;j>=a[i];j--)
                {
                    dp1[j]=(dp1[j]+dp1[j-a[i]])%mod;
                }
            }
            for(int i=0;i<m;i++)
            {
                for(int j=w;j>=b[i];j--)
                {
                    dp2[j]=(dp2[j]+dp2[j-b[i]])%mod;
                }
            }
            ll ans=0;
            for(int i=(w-k)/2+((w-k)%2?1:0);i<=(w+k)/2;i++)//注意下界
            {
                ans=(ans+dp1[i]*dp2[w-i])%mod;
            }
            printf("%lld
    ",ans);
        } 
        return 0;
    }
  • 相关阅读:
    MyBatis(五)动态SQL 之 foreach 标签
    MyBatis(五)动态SQL 之 bind绑定
    MyBatis(五)动态SQL 之 sql 标签(重用片段)
    MyBatis(五)动态SQL 之 choose(when、otherwise)标签
    Interesting Finds: 2008.04.19
    Interesting Finds: 2008.04.15
    Interesting Finds: 2008.04.22
    Interesting Finds: 2008.04.18
    Interesting Finds: 2008.04.17
    Interesting Finds: 2008.04.23
  • 原文地址:https://www.cnblogs.com/widsom/p/6739950.html
Copyright © 2011-2022 走看看