bzoj-1042 Coin Shopping

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.