5 * Solution for NEERC'2009 Problem K: K-Equivalence
6 * This solution checks correctness of the input.
7 * @author Roman Elizarov
9 public class kequiv_re
{
10 static final boolean DEBUG
= false;
12 public static void main(String
[] args
) throws Exception
{
16 void go() throws Exception
{
26 void read() throws Exception
{
27 Scanner in
= new Scanner(new File("kequiv.in"));
28 in
.useLocale(Locale
.US
);
31 assert n
>= 1 && n
<= 10000;
34 for (int i
= 0; i
< n
; i
++) {
38 assert a
[i
] >= 1 && a
[i
] <= b
[i
] && b
[i
] <= 1000000000000000000L;
39 assert i
== 0 || a
[i
] >= b
[i
- 1] + 2;
45 long[] p10
= new long[19];
46 long[] s
= new long[1];
51 int[] cn
= new int[11];
55 for (int i
= 1; i
< p10
.length
; i
++) {
56 p10
[i
] = p10
[i
- 1] * 10;
58 for (int i
= 0; i
< n
; i
++) {
60 long b
= this.b
[i
] + 1;
63 while (t
+ 1 < p10
.length
&& a
+ p10
[t
+ 1] <= b
&& a
% p10
[t
+ 1] == 0)
69 for (int p
= 1; p
< 10; p
++) {
77 void append(long s0
, int t0
) {
78 int capacity
= s
.length
;
80 s
= Arrays
.copyOf(s
, capacity
* 2);
81 t
= Arrays
.copyOf(t
, capacity
* 2);
87 System
.out
.println(s0
+ " - " + (s0
+ p10
[t0
] - 1));
92 for (int q
= 1; q
< 10; q
++) {
93 if (cn
[q
] == 0 && equiv(p
, q
))
98 private boolean equiv(int p
, int q
) {
99 for (int i
= 0; i
< m
; i
++) {
102 for (int u
= t
; u
<= 18; u
++) {
105 int d
= (int)(s
/ p10
[u
] % 10);
108 ss
+= p10
[u
] * (q
- p
);
110 ss
+= p10
[u
] * (p
- q
);
114 return false; // any shift by multiple of 10^18 is always out of range
115 int j
= Arrays
.binarySearch(a
, 0, n
, ss
);
118 if (j
< 0 || j
>= n
|| ss
+ p10
[t
] - 1 > b
[j
])
119 return false; // out of range
125 void write() throws Exception
{
126 PrintWriter out
= new PrintWriter("kequiv.out");
127 boolean[] w
= new boolean[11];
128 for (int p
= 1; p
< 10; p
++) {
130 for (int q
= p
; q
< 10; q
++) {
131 if (cn
[q
] == cn
[p
]) {
142 //----------------- just for validation ------------------
145 * Strict scanner to veryfy 100% correspondence between input files and input file format specification.
146 * It is a drop-in replacement for {@link java.util.Scanner} that could be added to a soulution source
147 * (cut-and-paste) without breaking its ability to work with {@link java.util.Scanner}.
149 public class Scanner
{
150 private final BufferedReader in
;
151 private String line
= "";
154 private boolean localeset
;
156 public Scanner(File source
) throws FileNotFoundException
{
157 in
= new BufferedReader(new FileReader(source
));
161 public void close() {
162 assert line
== null : "Extra data at the end of file";
165 } catch (IOException e
) {
166 throw new AssertionError("Failed to close with " + e
);
170 public void nextLine() {
171 assert line
!= null : "EOF";
172 assert pos
== line
.length() : "Extra characters on line " + lineNo
;
174 line
= in
.readLine();
175 } catch (IOException e
) {
176 throw new AssertionError("Failed to read line with " + e
);
182 public String
next() {
183 assert line
!= null : "EOF";
184 assert line
.length() > 0 : "Empty line " + lineNo
;
186 assert line
.charAt(0) > ' ' : "Line " + lineNo
+ " starts with whitespace";
188 assert pos
< line
.length() : "Line " + lineNo
+ " is over";
189 assert line
.charAt(pos
) == ' ' : "Wrong whitespace on line " + lineNo
;
191 assert pos
< line
.length() : "Line " + lineNo
+ " is over";
192 assert line
.charAt(0) > ' ' : "Line " + lineNo
+ " has double whitespace";
194 StringBuilder sb
= new StringBuilder();
195 while (pos
< line
.length() && line
.charAt(pos
) > ' ')
196 sb
.append(line
.charAt(pos
++));
197 return sb
.toString();
200 public int nextInt() {
202 assert s
.length() == 1 || s
.charAt(0) != '0' : "Extra leading zero in number " + s
+ " on line " + lineNo
;
203 assert s
.charAt(0) != '+' : "Extra leading '+' in number " + s
+ " on line " + lineNo
;
205 return Integer
.parseInt(s
);
206 } catch (NumberFormatException e
) {
207 throw new AssertionError("Malformed number " + s
+ " on line " + lineNo
);
211 public long nextLong() {
213 assert s
.length() == 1 || s
.charAt(0) != '0' : "Extra leading zero in number " + s
+ " on line " + lineNo
;
214 assert s
.charAt(0) != '+' : "Extra leading '+' in number " + s
+ " on line " + lineNo
;
216 return Long
.parseLong(s
);
217 } catch (NumberFormatException e
) {
218 throw new AssertionError("Malformed number " + s
+ " on line " + lineNo
);
222 public double nextDouble() {
223 assert localeset
: "Locale must be set with useLocale(Locale.US)";
225 assert s
.length() == 1 || s
.startsWith("0.") || s
.charAt(0) != '0' : "Extra leading zero in number " + s
+ " on line " + lineNo
;
226 assert s
.charAt(0) != '+' : "Extra leading '+' in number " + s
+ " on line " + lineNo
;
228 return Double
.parseDouble(s
);
229 } catch (NumberFormatException e
) {
230 throw new AssertionError("Malformed number " + s
+ " on line " + lineNo
);
234 public void useLocale(Locale locale
) {
235 assert locale
== Locale
.US
;