Advertisement
Guest User

Untitled

a guest
Apr 1st, 2012
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.20 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.math.*;
  4.  
  5. public class F {
  6.     public static void main(String[] args) throws IOException {
  7.         new F().run();
  8.     }
  9.    
  10.     void run() throws IOException {
  11.         //Scanner in = new Scanner(System.in);
  12.         //PrintWriter out = new PrintWriter(System.out);
  13.         Scanner in = new Scanner(new File("oranges.in"));
  14.         PrintWriter out = new PrintWriter(new File("oranges.out"));
  15.         BigInteger p, n;
  16.         p = in.nextBigInteger();
  17.         n = in.nextBigInteger();
  18.        
  19.         BigInteger start = BigInteger.ONE;
  20.         int k = -1;
  21.         for (int i = 0; ; ++i) {
  22.             if (start.compareTo(n) > 0) {
  23.                 k = i - 1;
  24.                 //out.println(i);
  25.                 start = start.divide(p);
  26.                 break;
  27.             }
  28.             start = start.multiply(p);
  29.         }
  30.        
  31.         BigInteger res1, res2, res3;
  32.         res1 = BigInteger.ZERO;
  33.         res2 = BigInteger.ZERO;
  34.         res3 = BigInteger.ZERO;
  35.        
  36.         //out.println(start);
  37.         //out.println(k);
  38.        
  39.         {
  40.             BigInteger A = n.subtract(start);
  41.             long a[] = new long [5000];
  42.             for (int i = 0; i < 5000; ++i) {
  43.                 a[i] = 0;
  44.             }
  45.            
  46.             //out.print(A + " ");
  47.            
  48.             int index = 0;
  49.             while (A.compareTo(BigInteger.ZERO) > 0) {
  50.                 a[index ++] = (A.remainder(p)).longValue();
  51.                 A = A.divide(p);
  52.             }
  53.            
  54.            
  55.            
  56.             int last_non_zero = -1;
  57.             for (int i = 0; i < 5000; ++i)
  58.                 if (a[i] != 0) last_non_zero = i;
  59.             if (last_non_zero == -1) {
  60.                 res1 = BigInteger.valueOf(k);
  61.             } else {
  62.                 long q = p.longValue();
  63.                 long coef = 1;
  64.                 for (int i = 0; i + 1 <= last_non_zero; ++i)
  65.                     if (a[i] > 0) {
  66.                         a[i] -= q;
  67.                         if (a[i] < -2) coef = 0;
  68.                         ++a[i + 1];
  69.                        
  70.                         if (a[i] == -1) {
  71.                             coef *= 2L;
  72.                             coef %= 1000000007L;
  73.                         }
  74.                     }
  75.                 if (a[last_non_zero] == 1 && last_non_zero < k) res1 = res1.add(BigInteger.valueOf(coef));
  76.                 for (int i = last_non_zero; i + 1 <= k - 1; ++i) {
  77.                     a[i] -= q;
  78.                     if (a[i] < -2) coef = 0;
  79.                     ++a[i + 1];
  80.                     if (a[i] == -1) {
  81.                         coef *= 2L;
  82.                         coef %= 1000000007L;
  83.                     }
  84.                     if (a[i + 1] == 1) res1 = res1.add(BigInteger.valueOf(coef));
  85.                 }
  86.             }
  87.         }
  88.        
  89.         {
  90.             BigInteger A = start.add(start).subtract(n);
  91.             if (A.compareTo(BigInteger.ZERO) <= 0) {
  92.             } else {
  93.                 long a[] = new long [5000];
  94.                 for (int i = 0; i < 5000; ++i) {
  95.                     a[i] = 0;
  96.                 }
  97.                
  98.                 int index = 0;
  99.                 while (A.compareTo(BigInteger.ZERO) > 0) {
  100.                     a[index ++] = (A.remainder(p)).longValue();
  101.                     A = A.divide(p);
  102.                 }
  103.                
  104.                 int ones = 0;
  105.                 for (int i = 0; i < 5000; ++i)
  106.                     if (a[i] == 1) ++ones;
  107.                
  108.                 if (ones != 0) {                
  109.                     int last_non_zero = -1;
  110.                     for (int i = 0; i < 5000; ++i)
  111.                         if (a[i] != 0) last_non_zero = i;
  112.                    
  113.                     long coef = 1;
  114.                     int d = -1;
  115.                     for (int i = 0; i < 5000; ++i)
  116.                         if (a[i] > 2) coef = 0; else {
  117.                             if (a[i] == 1) {
  118.                                 ++d;
  119.                                 coef *= 2L;
  120.                                 coef %= 1000000007L;
  121.                             }
  122.                         }
  123.                     if (coef != 0 && last_non_zero <= k - 1) {
  124.                         coef = 1;
  125.                         for (int i = 0; i < d; ++i) {
  126.                             coef *= 2L;
  127.                             coef %= 1000000007L;
  128.                         }
  129.                         res2 = BigInteger.valueOf(coef);
  130.                     }
  131.                     //if (last_non_zero <= k - 1) {
  132.                         //res2 = BigInteger.valueOf(coef);
  133.                     //}
  134.                 }
  135.             }
  136.         }
  137.        
  138.         {
  139.             BigInteger A = (start.multiply(p)).subtract(n);
  140.             long a[] = new long [5000];
  141.             for (int i = 0; i < 5000; ++i) {
  142.                 a[i] = 0;
  143.             }
  144.            
  145.             //out.println(A);
  146.             //out.println(k);
  147.             int index = 0;
  148.             while (A.compareTo(BigInteger.ZERO) > 0) {
  149.                 a[index ++] = (A.remainder(p)).longValue();
  150.                 A = A.divide(p);
  151.             }
  152.            
  153.             int last_non_zero = -1;
  154.             for (int i = 0; i < 5000; ++i)
  155.                 if (a[i] != 0) last_non_zero = i;
  156.            
  157.             long coef = 1;
  158.             int zero = 0;
  159.             for (int i = 0; i < k + 1; ++i) {
  160.                 if (a[i] == 1) {
  161.                     coef *= 2L;
  162.                     coef %= 1000000007L;
  163.                 }
  164.                 if (a[i] > 2) {
  165.                     coef = 0;
  166.                 }
  167.                 if (a[i] == 0) ++zero;
  168.             }
  169.             coef *= (long)(zero);
  170.             coef %= 1000000007L;
  171.             long coef3 = 1;
  172.             int nice[] = new int [5000];
  173.             for (int i = 0; i < 5000; ++i)
  174.                 nice[i] = 1;
  175.             for (int i = k; i >= 0; --i) {
  176.                 if (nice[i + 1] == 0) nice[i] = 0;
  177.                 if (a[i] > 1) nice[i] = 0;
  178.             }
  179.             for (int i = 0; i < k + 1; ++i) {
  180.                 if (a[i] == 0) {
  181.                     if (last_non_zero <= k && nice[i + 1] == 1)
  182.                         res3 = res3.add(BigInteger.valueOf(coef3));
  183.                 }
  184.                 if (a[i] == 1) {
  185.                     coef3 *= 2L;
  186.                     coef3 %= 1000000007L;
  187.                 }
  188.             }
  189.            
  190.             //out.println(res3);
  191.             //res3 = res3.add(BigInteger.valueOf(coef));
  192.             if (last_non_zero == -1) {
  193.             } else {
  194.                 long q = p.longValue();
  195.                 long coef2 = 1;
  196.                 //a[last_non_zero] -= q;
  197.                 //++a[last_non_zero + 1];
  198.                
  199.                 for (int i = 0; i < 5000; ++i)
  200.                     nice[i] = 1;
  201.                 for (int i = k; i >= 0; --i) {
  202.                     if (nice[i + 1] == 0) nice[i] = 0;
  203.                     if (a[i] > 1) nice[i] = 0;
  204.                 }
  205.                
  206.                 for (int i = 0; i <= last_non_zero; ++i) {
  207.                     if (a[i] == q - 1 && a[i + 1] == 0 && nice[i + 1] == 1) {
  208.                         if (i == last_non_zero) {
  209.                             if (last_non_zero + 1 <= k)
  210.                                 res3 = res3.add(BigInteger.valueOf(coef2));
  211.                         } else {
  212.                             if (last_non_zero <= k)
  213.                                 res3 = res3.add(BigInteger.valueOf(coef2));
  214.                         }
  215.                     }
  216.                     if (a[i] > 2) coef2 = 0;
  217.                     if (a[i] == 1) {
  218.                         coef2 *= 2L;
  219.                         coef2 %= 1000000007L;
  220.                     }
  221.                 }
  222.                 if (a[last_non_zero] != -1) coef2 = 0;
  223.                
  224.                 //if (last_non_zero + 1 <= k)
  225.                 //    res3 = res3.add(BigInteger.valueOf(coef2));
  226.             }
  227.         }
  228.        
  229.         //out.println(res1);
  230.         //out.println(res2);
  231.         //out.println(res3);
  232.        
  233.         BigInteger res = res1.add(res2.add(res3));
  234.         out.println(res.remainder(BigInteger.valueOf(1000000007L)));
  235.        
  236.         out.close();
  237.     }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement