-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUva11955.java
More file actions
73 lines (65 loc) · 2.08 KB
/
Copy pathUva11955.java
File metadata and controls
73 lines (65 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.*;
import java.io.*;
import java.math.BigInteger ;
import static java.lang.Math.* ;
public class Uva11955
{
static BigInteger [][] memo = new BigInteger[51][51];
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pr = new PrintWriter(System.out);
StringBuilder sb = new StringBuilder();
int tc = Integer.parseInt(br.readLine());
for(int t = 1 ; t <= tc ; t++) {
sb.append("Case ").append(t).append(": ");
String [] s = br.readLine().split("\\(|\\+|\\)|\\^");
String var1 = s[1];
String var2 = s[2];
int k = Integer.parseInt(s[4]);
for(int i = 0 ; i <= k ; i++){
// C(k,i)
if( memo[k][i] == null ){
memo[k][min(i,k-i)] = memo[k][max(i,k-i)] = C(k,i);
}
if( memo[k][i].compareTo(BigInteger.ONE) > 0 ){
sb.append(memo[k][i].toString()).append("*");
}
if( k-i > 0 ){
sb.append(var1);
if( k -i > 1 ){
sb.append("^").append(k-i);
}
}
if( i > 0 ){
if( k - i > 0 ) {
sb.append("*");
}
sb.append(var2);
if( i > 1 ){
sb.append("^").append(i);
}
}
if( i < k ) {
sb.append("+");
}
}
sb.append('\n');
}
pr.print(sb.toString());
pr.close();
br.close();
}
static BigInteger C(int n , int r)
{
return P(n,r).divide( P(r,r) ) ;
}
static BigInteger P(int n , int r)
{
BigInteger ans = BigInteger.ONE ;
for(int i = n ; i > n-r ; i--){
ans = ans.multiply(BigInteger.valueOf(i));
}
return ans ;
}
}