Submission #857420


Source Code Expand

Copy
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.InputMismatchException;

public class Main {
	InputStream is;

	int __t__ = 1;
	int __f__ = 0;
	int __FILE_DEBUG_FLAG__ = __f__;
	String __DEBUG_FILE_NAME__ = "src/C1";

	FastScanner in;
	PrintWriter out;

	long f(long b, long n) {
		if (n < b) return n;
		return f(b, (long)Math.floor(n / b)) + n % b;
	}
	public void solve() {
		long n = in.nextLong(), s = in.nextLong();
		if (n == s) {
			System.out.println(n+1);
			return;
		} else if (n < s) {
			System.out.println(-1);
			return;
		}
		for (long b = 2; b * b <= n; b++) {
			if (f(b, n) == s) {
				System.out.println(b);
				return;
			}
		}
		long res = Long.MAX_VALUE;
		for (long p = 2; p * p <= n; p++) {
			long b = (n - s) / p + 1;
			if (f(b, n) == s) 
				res = Math.min(b, res);
		}
		System.out.println(res != Long.MAX_VALUE ? res : -1);
		out.close();
	}

	public void run() {
		if (__FILE_DEBUG_FLAG__ == __t__) {
			try {
				is = new FileInputStream(__DEBUG_FILE_NAME__);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			System.out.println("FILE_INPUT!");
		} else {
			is = System.in;
		}
		in = new FastScanner(is);
		out = new PrintWriter(System.out);

		Thread t = new Thread(null, new Runnable() {
			
			@Override
			public void run() {
				solve();
			}
		}, "lul", 1 << 27);
		t.start();
	}

	public static void main(String[] args) {
		new Main().run();
	}

	public void mapDebug(int[][] a) {
		System.out.println("--------map display---------");

		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[i].length; j++) {
				System.out.printf("%3d ", a[i][j]);
			}
			System.out.println();
		}

		System.out.println("----------------------------");
		System.out.println();
	}

	public void debug(Object... obj) {
		System.out.println(Arrays.deepToString(obj));
	}

	class FastScanner {
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

		public FastScanner(InputStream stream) {
			this.stream = stream;
			//stream = new FileInputStream(new File("dec.in"));

		}

		int read() {
			if (numChars == -1)
				throw new InputMismatchException();
			if (curChar >= numChars) {
				curChar = 0;
				try {
					numChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		boolean isEndline(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		int[] nextIntArray(int n) {
			int[] array = new int[n];
			for (int i = 0; i < n; i++)
				array[i] = nextInt();

			return array;
		}

		int[][] nextIntMap(int n, int m) {
			int[][] map = new int[n][m];
			for (int i = 0; i < n; i++) {
				map[i] = in.nextIntArray(m);
			}
			return map;
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		long[] nextLongArray(int n) {
			long[] array = new long[n];
			for (int i = 0; i < n; i++)
				array[i] = nextLong();

			return array;
		}

		long[][] nextLongMap(int n, int m) {
			long[][] map = new long[n][m];
			for (int i = 0; i < n; i++) {
				map[i] = in.nextLongArray(m);
			}
			return map;
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		double[] nextDoubleArray(int n) {
			double[] array = new double[n];
			for (int i = 0; i < n; i++)
				array[i] = nextDouble();

			return array;
		}

		double[][] nextDoubleMap(int n, int m) {
			double[][] map = new double[n][m];
			for (int i = 0; i < n; i++) {
				map[i] = in.nextDoubleArray(m);
			}
			return map;
		}

		String next() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		String[] nextStringArray(int n) {
			String[] array = new String[n];
			for (int i = 0; i < n; i++)
				array[i] = next();

			return array;
		}

		String nextLine() {
			int c = read();
			while (isEndline(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndline(c));
			return res.toString();
		}
	}
}


Submission Info

Submission Time
Task D - Digit Sum
User hiro116s
Language Java8 (OpenJDK 1.8.0)
Score 0
Code Size 4780 Byte
Status WA
Exec Time 875 ms
Memory 140864 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status
AC × 4
WA × 1
AC × 36
WA × 11
Set Name Test Cases
Sample subtask1_87654_30.txt, subtask1_87654_138.txt, subtask1_87654_45678.txt, subtask1_31415926535_1.txt, subtask1_1_31415926535.txt
All subtask1_100000000000_1.txt, subtask1_100000000000_100000000000.txt, subtask1_100000000000_2.txt, subtask1_100000000000_3.txt, subtask1_100000000000_50000000000.txt, subtask1_100000000000_50000000001.txt, subtask1_100000000000_99999999999.txt, subtask1_16983563041_1.txt, subtask1_1_1.txt, subtask1_1_2.txt, subtask1_1_31415926535.txt, subtask1_239484768_194586924.txt, subtask1_2_1.txt, subtask1_2_2.txt, subtask1_31415926535_1.txt, subtask1_49234683534_2461734011.txt, subtask1_4_1.txt, subtask1_58640129658_232122496.txt, subtask1_68719476735_35.txt, subtask1_68719476735_36.txt, subtask1_68719476735_37.txt, subtask1_68719476736_1.txt, subtask1_68719476736_2.txt, subtask1_72850192441_16865701.txt, subtask1_79285169301_27.txt, subtask1_82914867733_1676425945.txt, subtask1_8594813796_75700.txt, subtask1_87654_12345.txt, subtask1_87654_138.txt, subtask1_87654_30.txt, subtask1_87654_4294967308.txt, subtask1_87654_45678.txt, subtask1_97822032312_49157112.txt, subtask1_98750604051_977728851.txt, subtask1_99999515529_1.txt, subtask1_99999515529_316226.txt, subtask1_99999515529_316227.txt, subtask1_99999515529_316228.txt, subtask1_99999515529_49999757765.txt, subtask1_99999515529_49999757766.txt, subtask1_99999515530_2.txt, subtask1_99999999977_1.txt, subtask1_99999999977_2.txt, subtask1_99999999977_49999999989.txt, subtask1_99999999977_49999999990.txt, subtask1_99999999999_1.txt, subtask1_99999999999_100000000000.txt
Case Name Status Exec Time Memory
subtask1_100000000000_1.txt AC 161 ms 8132 KB
subtask1_100000000000_100000000000.txt AC 164 ms 8184 KB
subtask1_100000000000_2.txt AC 214 ms 8808 KB
subtask1_100000000000_3.txt WA 221 ms 8768 KB
subtask1_100000000000_50000000000.txt WA 245 ms 8772 KB
subtask1_100000000000_50000000001.txt AC 230 ms 8804 KB
subtask1_100000000000_99999999999.txt WA 875 ms 140864 KB
subtask1_16983563041_1.txt AC 155 ms 8128 KB
subtask1_1_1.txt AC 161 ms 8052 KB
subtask1_1_2.txt AC 157 ms 8144 KB
subtask1_1_31415926535.txt AC 156 ms 8052 KB
subtask1_239484768_194586924.txt AC 170 ms 8168 KB
subtask1_2_1.txt WA 159 ms 8144 KB
subtask1_2_2.txt AC 160 ms 8140 KB
subtask1_31415926535_1.txt WA 212 ms 8808 KB
subtask1_49234683534_2461734011.txt WA 212 ms 8772 KB
subtask1_4_1.txt AC 161 ms 8176 KB
subtask1_58640129658_232122496.txt AC 220 ms 8808 KB
subtask1_68719476735_35.txt AC 173 ms 8772 KB
subtask1_68719476735_36.txt AC 150 ms 8180 KB
subtask1_68719476735_37.txt AC 216 ms 8808 KB
subtask1_68719476736_1.txt AC 151 ms 8176 KB
subtask1_68719476736_2.txt AC 164 ms 8164 KB
subtask1_72850192441_16865701.txt AC 219 ms 8804 KB
subtask1_79285169301_27.txt AC 163 ms 8052 KB
subtask1_82914867733_1676425945.txt AC 222 ms 8804 KB
subtask1_8594813796_75700.txt AC 160 ms 8168 KB
subtask1_87654_12345.txt AC 153 ms 8168 KB
subtask1_87654_138.txt AC 160 ms 8164 KB
subtask1_87654_30.txt AC 151 ms 8184 KB
subtask1_87654_4294967308.txt AC 149 ms 8184 KB
subtask1_87654_45678.txt AC 154 ms 8168 KB
subtask1_97822032312_49157112.txt AC 216 ms 8808 KB
subtask1_98750604051_977728851.txt AC 223 ms 8804 KB
subtask1_99999515529_1.txt AC 192 ms 8680 KB
subtask1_99999515529_316226.txt AC 219 ms 8804 KB
subtask1_99999515529_316227.txt AC 231 ms 8808 KB
subtask1_99999515529_316228.txt AC 219 ms 8808 KB
subtask1_99999515529_49999757765.txt WA 219 ms 8772 KB
subtask1_99999515529_49999757766.txt AC 223 ms 8772 KB
subtask1_99999515530_2.txt AC 194 ms 8676 KB
subtask1_99999999977_1.txt WA 217 ms 8808 KB
subtask1_99999999977_2.txt WA 230 ms 8768 KB
subtask1_99999999977_49999999989.txt WA 232 ms 8768 KB
subtask1_99999999977_49999999990.txt AC 231 ms 8772 KB
subtask1_99999999999_1.txt WA 224 ms 8804 KB
subtask1_99999999999_100000000000.txt AC 152 ms 8184 KB