Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigInteger;
- import java.util.StringTokenizer;
- import java.io.BufferedReader;
- import java.io.BufferedOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.io.OutputStream;
- public class Alien {
- static final int MOD=1<<20;
- static long powmod(long x, long y) {
- long res = 1; // Initialize result
- x = x % MOD; // Update x if it is more than or
- // equal to p
- while (y > 0)
- {
- // If y is odd, multiply x with result
- if ((y & 1) != 0)
- res = (res*x) % MOD;
- // y must be even now
- y = y>>1; // y = y/2
- x = (x*x) % MOD;
- }
- return res;
- }
- static long col_sum(int i, int X, long [] fi_tbl) {
- long fi = fi_tbl[i];
- long res=fi;
- long prev = fi;
- long add_this = fi_tbl[X-1];
- for(int ii = 0; ii <X-1; ++ii) {
- prev = (prev * powmod(33, X) + add_this) % MOD;
- res += prev; res %= MOD;
- }
- return res;
- }
- // 107005050|00005252- construct right, construct left, multiply
- static BigInteger FastBigInt(String x) {
- int size = x.length();
- if (size <= 5) {
- return new BigInteger(x);
- }
- int last_half = size/2;
- int last_half_size = size - last_half;
- BigInteger lo = FastBigInt(x.substring(last_half,
- last_half + last_half_size)); // last half
- BigInteger hi = FastBigInt(x.substring(0, last_half)); // first half
- return lo.add(BigInteger.TEN.pow(last_half_size).multiply(hi));
- }
- public static void main(String [] args) throws IOException{
- Kattio io = new Kattio(System.in, System.out);
- int K, X;
- K = io.getInt(); X = io.getInt();
- String msg = io.r.readLine();
- //System.out.println(msg);
- // should be a bit more?
- long[] fi_tbl = new long[X];
- fi_tbl[0] = 1;
- for (int i=1; i<X; i++) {
- fi_tbl[i] = (33*fi_tbl[i-1] + 1) % MOD;
- }
- long l0 = col_sum(0, X, fi_tbl);
- long l1 = col_sum(1, X, fi_tbl);
- long abez = 1016801;
- long B = ((l1 - l0) * abez) % MOD;
- long A = l0 - B;
- long[] all_col_sums = new long[X];
- for (int i=0; i<X; i++) {
- //System.out.println(A + " " + B + " " + fi_tbl[i]);
- all_col_sums[i] = (((A + B*fi_tbl[i]) % MOD) + MOD) % MOD;
- //System.out.println(all_col_sums[i]);
- }
- //System.out.println("did compute col sums");
- StringBuilder s=new StringBuilder();
- for (long x: all_col_sums)
- s.append(""+x);
- // System.out.println("did build string");
- //BigInteger b= new BigInteger(s.toString());
- BigInteger b = FastBigInt(s.toString());
- //System.out.println("did build integer");
- String base27Java = b.toString(27);
- //System.out.println("did convert to base");
- StringBuilder buildRes = new StringBuilder();
- for (int i=0; i<msg.length(); i++) {
- int x = msg.charAt(i);
- x -= 'A';
- if (x < 0) x = 26; // space
- char y = base27Java.charAt(i);
- int yy = y >= 'a' ? y-'a' + 10 : y-'0';
- int res = (x+yy) % 27;
- char c = (char)(res + 'A');
- if (c > 'Z') c = ' ';
- buildRes.append(c);
- }
- io.println(buildRes.toString());
- io.close();
- //for (long x: all_col_sums)
- }
- }
- /** Simple yet moderately fast I/O routines.
- *
- * Example usage:
- *
- * Kattio io = new Kattio(System.in, System.out);
- *
- * while (io.hasMoreTokens()) {
- * int n = io.getInt();
- * double d = io.getDouble();
- * double ans = d*n;
- *
- * io.println("Answer: " + ans);
- * }
- *
- * io.close();
- *
- *
- * Some notes:
- *
- * - When done, you should always do io.close() or io.flush() on the
- * Kattio-instance, otherwise, you may lose output.
- *
- * - The getInt(), getDouble(), and getLong() methods will throw an
- * exception if there is no more data in the input, so it is generally
- * a good idea to use hasMoreTokens() to check for end-of-file.
- *
- * @author: Kattis
- */
- class Kattio extends PrintWriter {
- public Kattio(InputStream i) {
- super(new BufferedOutputStream(System.out));
- r = new BufferedReader(new InputStreamReader(i));
- }
- public Kattio(InputStream i, OutputStream o) {
- super(new BufferedOutputStream(o));
- r = new BufferedReader(new InputStreamReader(i));
- }
- public boolean hasMoreTokens() {
- return peekToken() != null;
- }
- public int getInt() {
- return Integer.parseInt(nextToken());
- }
- public double getDouble() {
- return Double.parseDouble(nextToken());
- }
- public long getLong() {
- return Long.parseLong(nextToken());
- }
- public String getWord() {
- return nextToken();
- }
- public BufferedReader r;
- private String line;
- private StringTokenizer st;
- private String token;
- private String peekToken() {
- if (token == null)
- try {
- while (st == null || !st.hasMoreTokens()) {
- line = r.readLine();
- if (line == null) return null;
- st = new StringTokenizer(line);
- }
- token = st.nextToken();
- } catch (IOException e) { }
- return token;
- }
- private String nextToken() {
- String ans = peekToken();
- token = null;
- return ans;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement