forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathF.java
More file actions
114 lines (92 loc) · 2.89 KB
/
F.java
File metadata and controls
114 lines (92 loc) · 2.89 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.io.*;
import java.math.*;
import java.util.*;
public class F {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(new File("lottery.in"));
PrintWriter pw = new PrintWriter(new File("lottery.out"));
Solver sol = new Solver();
sol.init();
while (sc.hasNext()) {
int n = sc.nextInt();
String s = sc.next();
pw.println(sol.getMax(n, s));
pw.println(sol.getMin(n, s));
}
sc.close(); pw.close();
}
}
class Fraction {
BigInteger x, y;
Fraction(BigInteger x, BigInteger y) {
this.x = x;
this.y = y;
BigInteger g = x.gcd(y);
this.x = this.x.divide(g);
this.y = this.y.divide(g);
}
public String toString() {
return x.toString() + "/" + y.toString();
}
Fraction mul(Fraction a) {
return new Fraction(x.multiply(a.x), y.multiply(a.y));
}
}
class Solver {
BigInteger[][] c = new BigInteger[66][66];
public void init() {
for(int i = 0; i <= 60; ++i)
for(int j = 0; j <= 60; ++j)
c[i][j] = BigInteger.ZERO;
for(int i = 0; i <= 60; ++i) {
c[i][0] = BigInteger.ONE;
for(int j = 1; j <= i; ++j) {
c[i][j] = c[i-1][j-1].add(c[i-1][j]);
}
}
}
int[] x = new int[66];
int[] r = new int[66];
int nx;
Fraction getMin(int n, String s) {
int m = s.length();
int minx = x[0];
for(int i = 0; i < nx; ++i)
if (x[i] < minx)
minx = x[i];
return new Fraction(
c[n - m + minx][minx],
c[n][m]
);
}
Fraction getMax(int n, String s) {
HashMap<Character, Integer> cnt = new HashMap<>();
for(int i = 0; i < s.length(); ++i) {
char c = s.charAt(i);
if (!cnt.containsKey(c)) cnt.put(c, 1);
else cnt.put(c, cnt.get(c) + 1);
}
nx = 0;
for(char c : cnt.keySet()) {
x[nx++] = cnt.get(c);
}
// for(int i = 0; i < nx; ++i)
// System.out.print(x[i] + " ");
// System.out.println();
for(int i = 0; i < nx; ++i)
r[i] = 0;
int m = s.length();
for(int turn = 0; turn < n - m; ++turn) {
int u = 0;
for(int i = 1; i < nx; ++i)
if ((x[i] + r[i] + 1) * (r[u] + 1)
> (x[u] + r[u] + 1) * (r[i] + 1))
u = i;
++r[u];
}
Fraction res = new Fraction(BigInteger.ONE, c[n][m]);
for(int i = 0; i < nx; ++i)
res = res.mul(new Fraction(c[x[i] + r[i]][x[i]], BigInteger.ONE));
return res;
}
}