Luogu P1450 coin shopping solution to a problem report

Topic Portal

[Title] effect

There are four denominations of coins $ c [1 ~ 4] $, $ tot $ times now buy something, every time you buy something will take $ d [1 ~ 4] $ a four kinds of face value of the coin respectively, the total price of things to buy to $ s $, seeking each time to buy just the thing (i.e., the sum of the coin denomination is exactly spent $ S $) program number.

[Analysis] ideas

First, if there is no limit to the number of coins, it is easy to use fully expect backpack. So we pretreated the number of coins, without limitation, the total number for each of the programs.

Then think about how to deal with quantitative restrictions, the assumption that there is only one coin limit on the number, then forced this coin exceeds the limit, then we have to use other coin denominations spell value to $ s- (d + 1) * c $. Because the limit is exceeded, it is clear that these programs are not legal, the program number $ f [s- (d + 1) * c] $. So we know the number of successful coin program there is a number that exceeds the limit (here is no guarantee that this is the only coin exceeds the limit), with $ f [s] $ subtracting the number of programs in all A coin exceeds the limit after that we this one can think of a few more than minus two kinds of coins at the same time the number exceeds the limit of the program. Here is a bit familiar? It is clear that this problem can be found to use the inclusion-exclusion resolved, so the final answer should be:

$ F [s] - $ A coin quantity exceeds the limit number of programs $ + $ embodiment two kinds of coins exceeds the limit of $ - $ three kinds of coins exceeds the limit number of programs $ + $ four kinds of coins exceeds the limit number of programs.

This $ over $ it!

【Code】

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<queue>
 7 #define g() getchar()
 8 #define rg register
 9 #define go(i,a,b) for(rg int i=a;i<=b;i++)
10 #define back(i,a,b) for(rg int i=a;i>=b;i--)
11 #define db double
12 #define ll long long
13 #define il inline
14 #define pf printf
15 #define mem(a,b) memset(a,b,sizeof(a))
16 using namespace std;
17 int fr(){
18     int w=0,q=1;
19     char ch=g();
20     while(ch<'0'||ch>'9'){
21         if(ch=='-') q=-1;
22         ch=g();
23     }
24     while(ch>='0'&&ch<='9') w=(w<<1)+(w<<3)+ch-'0',ch=g();
25     return w*q;
26 }
27 int c[5],d[5],tot,s;
28 ll f[100002],ans;
29 int main(){
30     //freopen("","r",stdin);
31     //freopen("","w",stdout);
32     go(i,1,4) c[i]=fr();tot=fr();
33     f[0]=1;
34     go(i,1,4) go(j,c[i],100000) f[j]+=f[j-c[i]];
35     while(tot--){
36         ans=0;
37         go(i,1,4) d[i]=fr();s=fr();
38         go(i,0,15){//2^4=16
39             int t=s,type=0;
40             go(j,1,4) if((i>>(j-1)) & 1 ) = T-C [J] * (D [J] + 1 ), type ^ = 1 ; // receiving repellent bitwise wording
 41              // current bit is 1, forced out of limits             
42 is              IF (T < 0 ) Continue ;
 43 is              IF ! (type) + ANS = F [T]; // the even type of coin to be added outside the limits embodiment 
44 is              the else ans- = F [T]; // odd type of coins exceeds the limit to subtract scheme 
45          }
 46 is          PF ( " % LLD \ n- " , ANS);
 47      }
 48      return  0 ;
 49 }
Code poke here

Guess you like

Origin www.cnblogs.com/THWZF/p/11592759.html