ZOJ 1442 Dinner is the ready capacity repulsion principle + Java large number

Source: Internet
Author: User

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=442

Solving

X1 + x2 + x3 + .... + xn = m

where Xi belongs to [L, R]

The number of different solutions.

This problem needs to be used in large numbers, be careful.

The principle is the same as it did before. . The number of solutions for each XI greater than or equal to Li is calculated first. About this, how to solve, look at:

Http://www.cnblogs.com/liuweimingcprogram/p/6091396.html

Then let it go, enumeration has a number of damage conditions, is greater than R, minus, two, add back.

Because each r is different, it can only be violent Dfs. Which is the complexity of 2^n.

So complexity requires 2^n * constants.

Recommend a few questions.

Http://www.cnblogs.com/liuweimingcprogram/p/6134521.html

Http://www.cnblogs.com/liuweimingcprogram/p/6135008.html

The same way of thinking.

/** To change the license header, choose License Headers in the Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. *//** * * @authorLiu*/ImportJava.util.*; Importjava.math.*;//large number of header files Public classMain {Static Final intMAXN = 15; Static int[] be =New int[MAXN]; Static int[] en =New int[MAXN]; Static intN, M; StaticBigInteger ans;  Public Static voidMain (string[] args) {Scanner input=NewScanner (system.in); intt =Input.nextint ();  while((t--) > 0) {//The return value must be booln =Input.nextint (); M=Input.nextint (); intsum = 0;  for(inti = 1; I <= N; ++i) {Be[i]=Input.nextint (); En[i]=Input.nextint (); Sum+=Be[i]; } ans= Biginteger.zero;//Qing 0DFS (1, sum, 0);        System.out.println (ANS); }    }     Public StaticBigInteger C (intNintm) {//that's a big number .        if(N < m)returnBiginteger.zero; if(n = = m)returnBiginteger.one; BigInteger ans=Biginteger.one; intmx = Math.max (n-m, M);//Call Maximum Value        intMI = n-MX;  for(inti = 1; I <= mi; ++i) {ans= Ans.multiply (biginteger.valueof (mx + i));//methods for converting to large numbersAns = ans.divide (biginteger.valueof (i));//remember to receive the return value        }        returnans; }     Public Static voidDfsintCurintTotintHas ) {        if(cur = = n + 1) {            if(has% 2 = = 1) {ans= Ans.subtract (C (M-tot + n-1, n-1)); } ElseAns = Ans.add (C (M-tot + n-1, n-1)); return; } DFS (cur+ 1, Tot-be[cur] + en[cur] + 1, has + 1); DFS (cur+ 1, Tot, has); }}
View Code

ZOJ 1442 Dinner is the ready capacity repulsion principle + Java large number

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.