Test instructions
There are four denominations of coins ci, to do tot shopping;
Every time you shop each coin has a di, ask to buy s yuan items there are several methods;
Exercises
There are only four coins, you can guess the complexity of the algorithm is not very large;
The normal backpack can not limit the di of this condition;
It is not possible to disassemble the di-coin complexity, and it is equally difficult to make a statistical plan;
So consider the principle of tolerance to simplify the problem;
Remove the limitations of Di, so that f[x] four coins to buy X-yuan items have several methods;
This f-array can be processed in the complexity of O (4*10^5);
Then reverse consider if the coin 1 exceeds the limit of D1, f[s-c1* (d1+1)] happens to be the number of schemes;
The total scheme minus the number of these schemes, the principle of tolerance to do a bit better;
I always felt a little strange about my own self-exclusion. But anyway mess can ac= =;
Seems to open long long;
Code:
#include <stdio.h> #include <string.h> #include <algorithm> #define N 100001using namespace std; typedef long LONG Ll;int c[4],d[4];ll f[n],ans;void slove (int now,int val) {int k=0;for (int i=0;i<4;i++) {if (now& (1 <<i)) {k^=1;val-=c[i]* (d[i]+1);}} if (val<0) return; ans+= (k?-1:1) *f[val];} int main () {int n,m,i,j,k;for (i=0;i<4;i++) scanf ("%d", C+i), scanf ("%d", &n), F[0]=1;for (i=0;i<4;i++) {for (j= 0;j<n;j++) f[j+c[i]]+=f[j];} for (i=1;i<=n;i++) {for (j=0;j<4;j++) scanf ("%d", d+j), scanf ("%d", &m), Ans=0;for (j=0;j<16;j++) slove (j,m );p rintf ("%lld\n", ans);} return 0;}
bzoj-1042 Coin Shopping