zoukankan      html  css  js  c++  java
  • 【bzoj1042】[HAOI2008]硬币购物-递推与动规-容斥原理

    硬币购物

     硬币购物一共有4种硬币。面值分别为c1,c2,c3,c4。某人去商店买东西,去了tot次。每次带di枚ci硬币,买s
    i的价值的东西。请问每次有多少种付款方法。

    Input

      第一行 c1,c2,c3,c4,tot 下面tot行 d1,d2,d3,d4,s,其中di,s<=100000,tot<=1000

    Output

      每次的方法数

    Sample Input

    1 2 5 10 2
    3 2 3 1 10
    1000 2 2 2 900

    Sample Output

    4
    27

    题解:

    就是先f[i]表示到达i这个价值的方案数,先不管限制,

    然后可以这样想,将所有方案巨鹿,然后减去c1超过限制的,减去c2超过限制的,减去c3超过限制的,减去c4超过限制的。

    这样可以容斥来做。

     

    这样想会不会少+

    比如 f[i-(d[i]+1)*c[i]]方案中已经超过了d[i]的限制,也就是后面可以不超过限制,

    但是这样情况不会存在,为什么呢,因为前面的超出了,不超出,+后面一定超出,就是总的超出方案,比如前面超出,后面未超出

    可以想成前面未超出,后面超出。这样想就可以了。

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cmath>
     5 #include<cstring>
     6 #define ll long long
     7 using namespace std;
     8 
     9 int tot;
    10 int c[7],b[7];
    11 ll ans,f[100007];
    12 
    13 void dfs(int x,int k,int sum)
    14 {
    15     if (sum<0) return;
    16     if (x==5)
    17     {
    18         if (k&1) ans-=f[sum];
    19         else ans+=f[sum];
    20         return;
    21     }
    22     dfs(x+1,k+1,sum-(b[x]+1)*c[x]);
    23     dfs(x+1,k,sum);
    24 }
    25 int main()
    26 {
    27     for (int i=1;i<=4;i++)
    28         scanf("%d",&c[i]);
    29     scanf("%d",&tot);
    30     f[0]=1;
    31     for (int i=1;i<=4;i++)
    32         for (int j=c[i];j<=100000;j++)
    33             f[j]+=f[j-c[i]];
    34     int x;
    35     for (int i=1;i<=tot;i++)
    36     {
    37         for (int j=1;j<=4;j++)
    38             scanf("%d",&b[j]);
    39         scanf("%d",&x);
    40         ans=0;
    41         dfs(1,0,x);
    42         printf("%lld
    ",ans);
    43     }
    44 }
  • 相关阅读:
    存储管理:
    小软件推荐:
    python学习之序列化
    python学习之串口编程
    python学习之常用模块
    Linux调试工具strace和gdb常用命令小结
    adjtimex和时钟的几个概念tick,freq,ppm,jiffies
    koji
    top -Hp pid 显示所有的线程
    Neighbour table overflow---- arp表溢出
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/7717416.html
Copyright © 2011-2022 走看看