Codeforces 284E Coin Troubles 思維+完全揹包計數

E. Coin Troubles
time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard output
In the Isle of Guernsey there are n different types of coins. For each i (1 ≤ i ≤ n), coin of type i is worth ai cents. It is possible that ai = ajfor some i and j (i ≠ j).

Bessie has some set of these coins totaling t cents. She tells Jessie q pairs of integers. For each i (1 ≤ i ≤ q), the pair bi, ci tells Jessie that Bessie has a strictly greater number of coins of type bi than coins of type ci. It is known that all bi are distinct and all ci are distinct.

Help Jessie find the number of possible combinations of coins Bessie could have. Two combinations are considered different if there is some i (1 ≤ i ≤ n), such that the number of coins Bessie has of type i is different in the two combinations. Since the answer can be very large, output it modulo 1000000007 (109 + 7).

If there are no possible combinations of coins totaling t cents that satisfy Bessie's conditions, output 0.

Input
The first line contains three space-separated integers, n, q and t (1 ≤ n ≤ 300; 0 ≤ q ≤ n; 1 ≤ t ≤ 105). The second line contains n space separated integers, a1, a2, ..., an (1 ≤ ai ≤ 105). The next q lines each contain two distinct space-separated integers, bi and ci(1 ≤ bi, ci ≤ n; bi ≠ ci).

It's guaranteed that all bi are distinct and all ci are distinct.

Output
A single integer, the number of valid coin combinations that Bessie could have, modulo 1000000007 (109 + 7).

Examples
input
4 2 17
3 1 2 5
4 2
3 4
output
3
input
3 2 6
3 1 1
1 2
2 3
output
0
input
3 2 10
1 2 3
1 2
2 1
output
0
Note
For the first sample, the following 3 combinations give a total of 17 cents and satisfy the given conditions: {0 of type 1, 1 of type 2, 3 of type 3, 2 of type 4}, {0, 0, 6, 1}, {2, 0, 3, 1}.

No other combinations exist. Note that even though 4 occurs in both bi and ci,  the problem conditions are still satisfied because all bi are distinct and all ci are distinct.

OJ

CodeForces - 284E  Coin Troubles

題意


有n種硬幣,現在我們需要湊出t價值的硬幣,我們每種硬幣都可以任取數量,不過要滿足q個約束。

對於每個約束,(a,b)表示需要a種類的硬幣使用的個數>b種類的硬幣。

思路

如果num[u]>num[v],我們可以得出:

  • u至少選一個,因爲如果u和v都不選,0,0也是不滿足題意的,這一步我們可以把總價值t減少a[u]
  • val[v]+=val[u],因爲t已經選擇了一個u,所以對於v來說再選跟v一樣數量的即可

最後注意談判一下環即可。

 

 

#include <bits/stdc++.h>
using namespace std;
const int N=505;
const int M=100005;
typedef long long LL;
const int mod = 1e9 + 7;
int dp[M],in[N],G[N],a[N];
int n,q,t,u,v;
int cal()
{
    while(q--)
    {
        int u=0;
        for(int i=1;i<=n;i++)
        {
           if(in[i]==0&&G[i]!=0)
           {
               u=i;
               break;
           }
        }

        if(u==0)
            return 0;
        t-=a[u];
        if(t<0)
            return 0;

        int v=G[u];
        G[u]=0;
        in[v]--;
        a[v]+=a[u];

    }

    dp[0]=1;
    for(int i=1;i<=n;i++)
        for(int j=a[i];j<=t;j++)
        {
            dp[j]=(dp[j]+dp[j-a[i]])%mod;
        }

    return dp[t];
}
int main()
{

    scanf("%d %d %d",&n,&q,&t);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d",&a[i]);
    }


    for(int i = 1; i <= q; i++)
    {
        scanf("%d %d",&u,&v);
        in[v]++;
        G[u]=v;
    }




    printf("%d\n",cal());
    return 0;
}


原文鏈接:https://blog.csdn.net/mengxiang000000/article/details/78204464

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章