Guest User

Untitled

a guest
Oct 3rd, 2015
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.29 KB | None | 0 0
  1. import java.io.*;
  2. import java.math.BigInteger;
  3.  
  4. public class binomial_count_vi {
  5.  
  6.     final static long MOD = 1000000007;
  7.  
  8.     public static void solve(Input in, PrintWriter out) throws IOException {
  9.         int p = in.nextInt();
  10.         int alpha = in.nextInt();
  11.         BigInteger A = new BigInteger(in.next()), bp = BigInteger.valueOf(p);
  12. //        A = BigInteger.TEN.pow(1000);
  13.         long time0 = System.currentTimeMillis();
  14.         int[] a = new int[3500];
  15.         for (int i = 0; i < a.length && A.signum() > 0; ++i) {
  16.             a[i] = A.mod(bp).intValue();
  17.             A = A.divide(bp);
  18.         }
  19.         System.err.println(System.currentTimeMillis() - time0);
  20.         int aLen = a.length;
  21.         while (aLen > 0 && a[aLen - 1] == 0) {
  22.             --aLen;
  23.         }
  24.         long[][] d = new long[4][a.length + 2];
  25.         long[][] d1 = new long[4][a.length + 2];
  26.         d[0][0] = 1L;
  27.         for (int it = 0; it < aLen; ++it) {
  28.             long[] cnt = new long[12];
  29.             for (int c = 0; c < 2; ++c) {
  30.                 cnt[c] += cnt(0, a[it], c, p);
  31.                 cnt[4 + c] += cnt(a[it], a[it] + 1, c, p);
  32.                 cnt[8 + c] += cnt(a[it] + 1, p, c, p);
  33.                 cnt[2 + c] += cnt(p, p + a[it], c, p);
  34.                 cnt[4 + 2 + c] += cnt(p + a[it], p + a[it] + 1, c, p);
  35.                 cnt[8 + 2 + c] += cnt(p + a[it] + 1, 2 * p, c, p);
  36.             }
  37.             for (int i = 0; i < cnt.length; ++i) {
  38.                 cnt[i] %= MOD;
  39.             }
  40.             for (int i = 0; i <= it; ++i) {
  41.                 for (int u = 0; u < 4; ++u) {
  42.                     if (d[u][i] == 0) {
  43.                         continue;
  44.                     }
  45.                     for (int t = u & 1; t < cnt.length; t += 2) {
  46.                         int v = 2 * mod(u / 2, t / 4) + ((t / 2) & 1);
  47.                         d1[v][i + (v & 1)] += d[u][i] * cnt[t];
  48.                     }
  49.                 }
  50.             }
  51.             for (int i = 0; i < d.length; ++i) {
  52.                 for (int j = 0; j <= it + 1; ++j) {
  53.                     d[i][j] = d1[i][j] % MOD;
  54.                     d1[i][j] = 0;
  55.                 }
  56.             }
  57.         }
  58.         long ans = 0;
  59.         for (int j = alpha; j < d[0].length; ++j) {
  60.                 ans += d[0][j];
  61.         }
  62.         out.println(ans % MOD);
  63.         System.err.println(System.currentTimeMillis() - time0);
  64.     }
  65.  
  66.     private static int mod(int u, int um) {
  67.         return um == 0 ? 0 : um == 2 ? 1 : u;
  68.     }
  69.  
  70.     private static long cnt(int from, int to, int c, long p) {
  71.         return cnt(to - c, p) - cnt(from - c, p);
  72.     }
  73.  
  74.     private static long cnt(long ai, long p) {
  75.         ai = Math.min(ai, 2 * p);
  76.         ai = Math.max(ai, 0);
  77.         return ai * (ai + 1) / 2 - (ai > p ? (ai - p) * (ai - p + 1) : 0);
  78.     }
  79.  
  80.     public static void main(String[] args) throws IOException {
  81.         PrintWriter out = new PrintWriter(System.out);
  82.         solve(new Input(new BufferedReader(new InputStreamReader(System.in))), out);
  83.         out.close();
  84.     }
  85.  
  86.     static class Input {
  87.         BufferedReader in;
  88.         StringBuilder sb = new StringBuilder();
  89.  
  90.         public Input(BufferedReader in) {
  91.             this.in = in;
  92.         }
  93.  
  94.         public Input(String s) {
  95.             this.in = new BufferedReader(new StringReader(s));
  96.         }
  97.  
  98.         public String next() throws IOException {
  99.             sb.setLength(0);
  100.             while (true) {
  101.                 int c = in.read();
  102.                 if (c == -1) {
  103.                     return null;
  104.                 }
  105.                 if (" \n\r\t".indexOf(c) == -1) {
  106.                     sb.append((char)c);
  107.                     break;
  108.                 }
  109.             }
  110.             while (true) {
  111.                 int c = in.read();
  112.                 if (c == -1 || " \n\r\t".indexOf(c) != -1) {
  113.                     break;
  114.                 }
  115.                 sb.append((char)c);
  116.             }
  117.             return sb.toString();
  118.         }
  119.  
  120.         public int nextInt() throws IOException {
  121.             return Integer.parseInt(next());
  122.         }
  123.  
  124.         public long nextLong() throws IOException {
  125.             return Long.parseLong(next());
  126.         }
  127.  
  128.         public double nextDouble() throws IOException {
  129.             return Double.parseDouble(next());
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment