Joj 1172 The Equation Problem combined mathematics

Source: Internet
Author: User

Question:

Mathematicians are able to solve Into a mythical problem. they indeed do much for the mankind. but sometimes they play trick to make some difficult problems for us to solve. the equation problem is one of them. consider the equation like this: X1 + X2 +... + Xn = P, where both of n (n <= 15) and P (P <= 30) are positive integers and x1... XN are unkowns. the task is for you given n positive integers A1... an, to write a program to determine how many solutions for this kind of equation for some pair of N and P, where xi> = AI.

For example, assume the equation is X1 + X2 = 5, and A1 = 2, a2 = 2. you must calculate the number of solutions which meet the conditions: x1> = A1 and X2> = A2. obviously, both of X1 = 2x2 = 3 and X1 = 3x2 = 2 satisfy the conditions. and no other solution at all. so the number of total solutions is 2. you need only answer the number.

Input Specification

The input consists of M equations, the first line of input is an integer M. Then follow M equations 'descriptions, each of which is in the form of below:

N p
A1 a2...

Integers are separated by spaces.

Output Specification

For each equation, you shocould print a single line containing the number of solutions.

 

Solution:

Due to the question requirements, the final equation form is:


Sum for each a, including:

Subtract the two formulas to obtain the following:

This equation has appeared in composite mathematics. The number of solutions is equal to the number of r groups in multiple sets with k types of elements. The result is equal:

For this question, r is p-a and k is n.

The Code is as follows:

  1. # Include <cstdio>
  2. Int main (){
  3. Freopen ("in.txt", "r", stdin );
  4. Int m, n, p;
  5. Scanf ("% d", & m );
  6. While (M --){
  7. Scanf ("% d", & N, & P );
  8. Int A, Suma = 0;
  9. For (INT I = 0; I <n; I ++ ){
  10. Scanf ("% d", & );
  11. SUMA + =;
  12. }
  13. If (P = SUMA ){
  14. Printf ("1/N ");
  15. Continue;
  16. }
  17. If (P <SUMA ){
  18. Printf ("0/N ");
  19. Continue;
  20. }
  21. Int x = p-Suma + n-1, y = (p-SUMA)> (n-1 )? (N-1) :( p-SUMA );
  22. Double ret = 1;
  23. For (INT I = 0; I <Y; I ++)
  24. RET * = (X-I );
  25. For (INT I = 2; I <= y; I ++)
  26. RET/= I;
  27. Printf ("%. 0lf/N", RET );
  28. }
  29. Return 0;
  30. }

 

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.