4 public class kequiv_al_slow
{
5 static final int MaxN
= 10000;
6 static final long MaxAB
= (long)1e18
;
7 static final int PL
= 19;
9 static long rangeLong (long x
, long a
, long b
) {
10 if (x
< a
|| x
> b
) throw new RuntimeException (x
+ " is not in range " + a
+ ".." + b
);
14 static long rangeLong (String s
, long a
, long b
) {
15 return rangeLong (Long
.parseLong (s
), a
, b
);
18 static long myLong (String s
) {
19 if (s
.length () == 0) return 0; else return Long
.parseLong (s
);
22 static int digdist (int d1
, int d2
) {
23 if (d2
> d1
) return d2
- d1
; else return d2
- d1
+ 10;
26 static boolean [][] G
;
31 static void dfs (int x
) {
33 for (int j
= 1; j
<= 9; j
++)
34 if (C
[j
] == 0 && G
[x
][j
])
39 public static void main (String args
[]) throws Exception
{
40 BufferedReader in
= new BufferedReader (new FileReader ("kequiv.in"));
41 PrintWriter out
= new PrintWriter ("kequiv.out");
42 int n
= Integer
.parseInt (in
.readLine ());
43 rangeLong (n
, 1, MaxN
);
45 long [] T
= new long [PL
];
47 for (int i
= PL
- 2; i
>= 0; i
--) T
[i
] = T
[i
+ 1] * 10;
49 long [] a
= new long [n
], b
= new long [n
];
50 char [][] ac
= new char [n
][], bc
= new char [n
][];
53 for (int i
= 0; i
< n
; i
++) {
54 StringTokenizer tok
= new StringTokenizer (in
.readLine ());
55 String as
= tok
.nextToken ();
56 String bs
= tok
.nextToken ();
57 a
[i
] = rangeLong (as
, last
+ 2, MaxAB
);
58 b
[i
] = rangeLong (bs
, a
[i
], MaxAB
);
59 char [] d
= new char [PL
- as
.length ()];
61 ac
[i
] = (new String (d
) + as
).toCharArray ();
62 d
= new char [PL
- bs
.length ()];
64 bc
[i
] = (new String (d
) + bs
).toCharArray ();
66 if (tok
.hasMoreTokens ()) throw new Exception ("EOL expected");
69 if (in
.ready ()) throw new Exception ("EOF expected");
71 long [][][] ast
= new long[n
][10][PL
], bfn
= new long [n
][10][PL
];
72 long [][][] aln
= new long[n
][10][PL
], bln
= new long [n
][10][PL
];
74 for (int i
= 0; i
< n
; i
++)
75 for (int d1
= 1; d1
<= 9; d1
++)
76 for (int p
= 0; p
< PL
; p
++) {
77 char [] cur
= ac
[i
].clone ();
78 //System.out.println (cur);
79 long fR
= T
[p
] - myLong (new String (Arrays
.copyOfRange (ac
[i
], p
+ 1, PL
)));
80 if (cur
[p
] == d1
+ '0') {
82 aln
[i
][d1
][p
] = Math
.min (fR
, b
[i
] - a
[i
] + 1);
84 ast
[i
][d1
][p
] = a
[i
] + fR
+ (digdist (cur
[p
] - '0', d1
) - 1) * T
[p
];
85 aln
[i
][d1
][p
] = Math
.min (T
[p
], b
[i
] - ast
[i
][d1
][p
] + 1);
87 //System.out.println (i + " " + d1 + " " + " " + p + ": " + fR + " " + ast[i][d1][p]);
90 fR
= myLong (new String (Arrays
.copyOfRange (bc
[i
], p
+ 1, PL
))) + 1;
91 if (cur
[p
] == d1
+ '0') {
93 bln
[i
][d1
][p
] = Math
.min (fR
, b
[i
] - a
[i
] + 1);
95 bfn
[i
][d1
][p
] = b
[i
] - fR
- (digdist (d1
, cur
[p
] - '0') - 1) * T
[p
];
96 bln
[i
][d1
][p
] = Math
.min (T
[p
], bfn
[i
][d1
][p
] - a
[i
] + 1);
101 G
= new boolean [10][10];
103 for (int i
= 1; i
<= 9; i
++)
104 for (int j
= 1; j
<= 9; j
++)
107 System
.out
.println (".1");
109 for (int d1
= 1; d1
<= 9; d1
++)
110 for (int d2
= 1; d2
<= 9; d2
++) {
111 for (int p
= 0; p
< PL
; p
++) {
113 for (int i
= 0; i
< n
; i
++) {
114 if (aln
[i
][d1
][p
] > 0) {
116 long tofind
= ast
[i
][d1
][p
] + (d2
- d1
) * T
[p
];
117 //System.out.println ("tofind = " + tofind);
118 while (j
< n
&& b
[j
] < tofind
) ++j
;
119 //System.out.println ("j = " + j + ", a[j] = " + a[j] + ", b[j] = " + b[j]);
120 if (j
== n
|| a
[j
] > tofind
) {
122 //System.out.println ("(" + p + "): " + ast[i][d1][p] + " -> " + bfn[i][d1][p] + " blocks " + d1 + " -> " + d2);
128 long toend
= bfn
[i
][d1
][p
] + (d2
- d1
) * T
[p
];
129 //System.out.println ("toend = " + toend);
130 while (j
< n
&& b
[j
] < toend
) {
134 } while (j
< n
&& aln
[j
][d2
][p
] <= 0);
138 /*if (ast[j][d2][p] - bfn[oj][d2][p] < 9 * T[p] + 1) {
139 throw new Exception ("bug detected (" + j + ", " + d2 + ", " + p + "): " + ast[j][d2][p] + " " + bfn[oj][d2][p]);
141 if (ast
[j
][d2
][p
] - bfn
[oj
][d2
][p
] != 9 * T
[p
] + 1) {
148 //System.out.println ("(" + p + "): " + ast[i][d1][p] + " -> " + bfn[i][d1][p] + " blocks " + d1 + " -> " + d2);
155 throw new RuntimeException ("bug detected");
162 System
.out
.println (".2");
167 for (int i
= 1; i
<= 9; i
++) {
174 for (int i
= 1; i
<= cc
; i
++) {
175 for (int j
= 1; j
<= 9; j
++) {