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;
    }
  • 相关阅读:
    java序列化和反序列化使用总结
    什么是N+1查询?
    Oracle insert /*+ APPEND */原理解析
    Oracle redo与undo
    MongoDB(三)-- 执行JS、界面工具
    几种Bean的复制方法性能比较
    Kafka(三)-- Kafka主要参数
    Kafka(二)-- 安装配置
    Kafka(一)-- 初体验
    Nginx(十二)-- Nginx+keepalived实现高可用
  • 原文地址:https://www.cnblogs.com/widsom/p/6739950.html
Copyright © 2011-2022 走看看