package jp.funnything.competition.util;

import java.math.BigInteger;

/**
 * Utility for BigInteger
 */
public class BI {
    public static BigInteger ZERO = BigInteger.ZERO;
    public static BigInteger ONE = BigInteger.ONE;

    public static BigInteger add( final BigInteger x , final BigInteger y ) {
        return x.add( y );
    }

    public static BigInteger add( final BigInteger x , final long y ) {
        return add( x , v( y ) );
    }

    public static BigInteger add( final long x , final BigInteger y ) {
        return add( v( x ) , y );
    }

    public static int cmp( final BigInteger x , final BigInteger y ) {
        return x.compareTo( y );
    }

    public static int cmp( final BigInteger x , final long y ) {
        return cmp( x , v( y ) );
    }

    public static int cmp( final long x , final BigInteger y ) {
        return cmp( v( x ) , y );
    }

    public static BigInteger div( final BigInteger x , final BigInteger y ) {
        return x.divide( y );
    }

    public static BigInteger div( final BigInteger x , final long y ) {
        return div( x , v( y ) );
    }

    public static BigInteger div( final long x , final BigInteger y ) {
        return div( v( x ) , y );
    }

    public static BigInteger mod( final BigInteger x , final BigInteger y ) {
        return x.mod( y );
    }

    public static BigInteger mod( final BigInteger x , final long y ) {
        return mod( x , v( y ) );
    }

    public static BigInteger mod( final long x , final BigInteger y ) {
        return mod( v( x ) , y );
    }

    public static BigInteger mul( final BigInteger x , final BigInteger y ) {
        return x.multiply( y );
    }

    public static BigInteger mul( final BigInteger x , final long y ) {
        return mul( x , v( y ) );
    }

    public static BigInteger mul( final long x , final BigInteger y ) {
        return mul( v( x ) , y );
    }

    public static BigInteger sub( final BigInteger x , final BigInteger y ) {
        return x.subtract( y );
    }

    public static BigInteger sub( final BigInteger x , final long y ) {
        return sub( x , v( y ) );
    }

    public static BigInteger sub( final long x , final BigInteger y ) {
        return sub( v( x ) , y );
    }

    public static BigInteger v( final long value ) {
        return BigInteger.valueOf( value );
    }
}

package org.sooo;

import java.io.File;
import java.io.OutputStreamWriter;
import java.util.List;

import com.google.common.base.Charsets;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.io.CharStreams;
import com.google.common.io.Files;
import com.google.common.io.OutputSupplier;

public class RecycledNumbers {

	public static boolean isValidPair(long n, long m) {
		String nStr = Long.toString(n);
		String mStr = Long.toString(m);
		for (int i = 0; i < nStr.length(); i++) {
			String shifted = nStr.substring(i) + nStr.substring(0, i);
			if (shifted.charAt(0) != '0' && shifted.equals(mStr))
				return true;
		}
		return false;
	}

	public static void main(String[] args) throws Exception {
		//String path = "src/main/resources/C-sample.in";
		String path = "src/main/resources/C-small-attempt0.in";
		List<String> lines = Files.readLines(new File(path), Charsets.UTF_8);

		OutputSupplier<OutputStreamWriter> outputSupplier = Files
				.newWriterSupplier(new File(path + ".out"), Charsets.UTF_8);
		StringBuilder results = new StringBuilder();
		for (int i = 1; i < lines.size(); i++) {
			List<String> tokens = Lists.newArrayList(Splitter.on(' ').split(
					lines.get(i)));
			long lowerBound = Long.parseLong(tokens.get(0));
			long upperBound = Long.parseLong(tokens.get(1));
			long count = 0;

			if (lowerBound >= 10) {
				for (long n = lowerBound; n < upperBound; n++) {
					for (long m = n + 1; m <= upperBound; m++) {
						if (RecycledNumbers.isValidPair(n, m))
							count++;
					}
				}
			}

			String result = String.format("Case #%d: %d\n", i, count);
			System.out.print(result);
			results.append(result);
		}
		CharStreams.write(results, outputSupplier);
	}
}
