Last active
February 7, 2016 06:57
-
-
Save guliash/09e0c2ad6cd2ae6fc529 to your computer and use it in GitHub Desktop.
acmp191
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.OutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.PrintWriter; | |
import java.util.InputMismatchException; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.io.InputStream; | |
/** | |
* Built using CHelper plug-in | |
* Actual solution is at the top | |
* | |
* @author Artem Gilmudinov | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
InputStream inputStream = System.in; | |
OutputStream outputStream = System.out; | |
InputReader in = new InputReader(inputStream); | |
PrintWriter out = new PrintWriter(outputStream); | |
TaskAcmpFast solver = new TaskAcmpFast(); | |
solver.solve(1, in, out); | |
out.close(); | |
} | |
static class TaskAcmpFast { | |
public void solve(int testNumber, InputReader in, PrintWriter out) { | |
long n = in.readLong(); | |
long[][] dp = new long[10][50]; | |
for (int i = 1; i < 10; i++) { | |
dp[i][1] = 1; | |
} | |
long cur = 9; | |
int len = 1; | |
while (true) { | |
if (cur >= n) { | |
break; | |
} | |
for (int i = 1; i < 10; i++) { | |
for (int z = i; z < 10; z++) { | |
dp[i][len + 1] += dp[z][len]; | |
} | |
} | |
for (int i = 1; i < 10; i++) { | |
cur += dp[i][len + 1]; | |
} | |
++len; | |
} | |
cur = n; | |
int prev = 0; | |
long pref = 0; | |
for (int i = 1; i < len; i++) { | |
for (int j = 1; j < 10; j++) { | |
cur -= dp[j][i]; | |
} | |
} | |
ArrayList<Integer> ans = new ArrayList<>(); | |
for (int i = len; i >= 1; i--) { | |
pref = 0; | |
for (int j = prev; j < 10; j++) { | |
pref += dp[j][i]; | |
if (pref >= cur) { | |
prev = j; | |
break; | |
} | |
} | |
ans.add(prev); | |
cur -= pref - dp[prev][i]; | |
} | |
for (int v : ans) { | |
out.print(v); | |
} | |
} | |
} | |
static class InputReader { | |
private InputStream stream; | |
private byte[] buf = new byte[1024]; | |
private int curChar; | |
private int numChars; | |
private SpaceCharFilter filter; | |
public InputReader(InputStream stream) { | |
this.stream = stream; | |
} | |
public 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++]; | |
} | |
public long readLong() { | |
int c = read(); | |
while (isSpaceChar(c)) | |
c = read(); | |
int sgn = 1; | |
if (c == '-') { | |
sgn = -1; | |
c = read(); | |
} | |
long res = 0; | |
do { | |
if (c < '0' || c > '9') | |
throw new InputMismatchException(); | |
res *= 10; | |
res += c - '0'; | |
c = read(); | |
} while (!isSpaceChar(c)); | |
return res * sgn; | |
} | |
public boolean isSpaceChar(int c) { | |
if (filter != null) | |
return filter.isSpaceChar(c); | |
return isWhitespace(c); | |
} | |
public static boolean isWhitespace(int c) { | |
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; | |
} | |
public interface SpaceCharFilter { | |
public boolean isSpaceChar(int ch); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment