In mathematics, binomial coefficients are positive integers in the form of coefficients in the binomial theorem. NCk shows the number of items to be selected from different objects n to K objects.
However, when n and k are very large, we often save them after modulo operation by main number P. Please calculate how many binomial coefficients become 0 after modulo by P.
Input
The first integer of input is T, the number of test cases.
Each of the following T lines has 2 integers, n and prime p.
Output
For each test case, the output contains a number of NCKs (0 & lt; = k & lt; = n) in the line from which the modules After operation is 0.
sample input 3 2 2 3 2 4 3 sample output 1 0 1 Since the barriers are very large, dynamic programming will not work. I want all that is an idea.
This is a problem about mathematics.
Import java.io. *; Import java.math.BigInteger; Import java.util. *; Public class solutions {public static zero main (string [] args) {scanner scan = new scanner (System.in); Int t = integer Paracet (scan nxtine ()); Int i, j; Long P; Long time; Int [] Points = New Int [501]; Int [] odigit = new int [501]; Int [] res = new int [501]; Int [] ans = new int [501]; While (0! = (T -)) {string [] line = scan NXTIN () the division (""); P = Integer.parseInt (line [1]); (I = 0; i 1) {y = 0; I = 1; Ans [0] = points [0]; Whereas (i & lt; numeral [0]) {y = y * 10 + number [i]; Ans [i ++] = (int) ((double) y / (double) p); Y% = p; } Res [++ res [0]] = (int) y; I = 1; While (I & lt; nan [0] and ANS [i] == 0) I ++; Numbers [0] 1 =; For (j = i; j and lt; nan [0]; j ++) digits [points [0] ++] = ans [j]; } Res [0] ++; // §§æ ?? Degree celsius ?? Ah ?? BigIntergere odata = New Bigtegger (Line [0]); BigInteger pdata = New BigInteger ("1"); (I = 1; i [l] res [0]; i ++) pdata = pdata.multiply (new begiteger ((res [i] +1) + "")); Odata = odata.subtract (pdata) .add (new BigInteger ("1")); Println (odata.toString ()); }}}
Edit (shorten the code):
import java.math.BigInteger; Import java.util. *; Class Solutions {/ n} Private Static Arrests & lt; Integer & gt; ToBase (Bigtegger N, Bigenter B) {ArrayList & lt; Integer & gt; Out = new arreelist & lt; Integer & gt; (); While (! N.equals (BigInteger.ZERO)) {out.add (n.mod (b) .intValue ()); N = n.divide (b); } Exit; } Public static zero main (string [] args) {scanner scan = new scanner (System.in); Int T = scan.nextInt (); While ((T-F)> 1) {BigInteger N = Scan. NXTBG (); BigInteger p = scan.nextBigInteger (); ArrayList & LT; Integer & gt; Res = toBase (N, P); BigInteger pdata = BigInteger.ONE; For (int i: ridge) {pdata = pdata.multiply (BigInter.View (I + 1)); } BigInteger Output = n.subtract (pdata) .add (BigInteger.ONE); Println (production); }}}
3 2 2 3 2 4 3 sample output 1 0 1 Since the barriers are very large, dynamic programming will not work. I want all that is an idea.
This is a problem about mathematics.
Import java.io. *; Import java.math.BigInteger; Import java.util. *; Public class solutions {public static zero main (string [] args) {scanner scan = new scanner (System.in); Int t = integer Paracet (scan nxtine ()); Int i, j; Long P; Long time; Int [] Points = New Int [501]; Int [] odigit = new int [501]; Int [] res = new int [501]; Int [] ans = new int [501]; While (0! = (T -)) {string [] line = scan NXTIN () the division (""); P = Integer.parseInt (line [1]); (I = 0; i 1) {y = 0; I = 1; Ans [0] = points [0]; Whereas (i & lt; numeral [0]) {y = y * 10 + number [i]; Ans [i ++] = (int) ((double) y / (double) p); Y% = p; } Res [++ res [0]] = (int) y; I = 1; While (I & lt; nan [0] and ANS [i] == 0) I ++; Numbers [0] 1 =; For (j = i; j and lt; nan [0]; j ++) digits [points [0] ++] = ans [j]; } Res [0] ++; // §§æ ?? Degree celsius ?? Ah ?? BigIntergere odata = New Bigtegger (Line [0]); BigInteger pdata = New BigInteger ("1"); (I = 1; i [l] res [0]; i ++) pdata = pdata.multiply (new begiteger ((res [i] +1) + "")); Odata = odata.subtract (pdata) .add (new BigInteger ("1")); Println (odata.toString ()); }}}
Edit (shorten the code):
import java.math.BigInteger; Import java.util. *; Class Solutions {/ n} Private Static Arrests & lt; Integer & gt; ToBase (Bigtegger N, Bigenter B) {ArrayList & lt; Integer & gt; Out = new arreelist & lt; Integer & gt; (); While (! N.equals (BigInteger.ZERO)) {out.add (n.mod (b) .intValue ()); N = n.divide (b); } Exit; } Public static zero main (string [] args) {scanner scan = new scanner (System.in); Int T = scan.nextInt (); While ((T-F)> 1) {BigInteger N = Scan. NXTBG (); BigInteger p = scan.nextBigInteger (); ArrayList & LT; Integer & gt; Res = toBase (N, P); BigInteger pdata = BigInteger.ONE; For (int i: ridge) {pdata = pdata.multiply (BigInter.View (I + 1)); } BigInteger Output = n.subtract (pdata) .add (BigInteger.ONE); Println (production); }}}
Comments
Post a Comment