• 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;
    }
  • 相关阅读:
    删除datatable的重复行
    导出大Excel
    winform调用plugin
    System.AppDomain.CurrentDomain.BaseDirectory总是取得根目录
    能不能多想一点呢?
    执行语句使用exec (sql)
    open the folder
    取得一个表的所有字段
    快讯:麦考林第四季度净利110万美元同比减62%
    麦考林第四季度净利110万美元同比减62%(图解)
  • 原文地址:https://www.cnblogs.com/widsom/p/6739950.html
Copyright © 2020-2023  润新知