Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1.     private final static long MODULO = 1000 * 1000 * 1000 + 7;
  2.  
  3.     private void solve() {
  4.         int n = readInt();
  5.  
  6.         int a = readInt();
  7.         int b = readInt();
  8.  
  9.         LimitedQueueWithSum aQueue = new LimitedQueueWithSum(a);
  10.         LimitedQueueWithSum bQueue = new LimitedQueueWithSum(b);
  11.  
  12.         aQueue.add(1);
  13.         bQueue.add(1);
  14.  
  15.         for (int length = 2; length <= n; ++length) {
  16.             long aSum = aQueue.getSum();
  17.             long bSum = bQueue.getSum();
  18.  
  19.             aQueue.add(bSum);
  20.             bQueue.add(aSum);
  21.         }
  22.  
  23.         long answer = add(aQueue.getSum(), bQueue.getSum());
  24.         out.println(answer);
  25.     }
  26.  
  27.     static long add(long a, long b) {
  28.         return (a + b) % MODULO;
  29.     }
  30.  
  31.     static long subtract(long a, long b) {
  32.         return add(a, MODULO - b % MODULO);
  33.     }
  34.  
  35.     static class LimitedQueueWithSum {
  36.         final int sizeLimit;
  37.  
  38.         Queue<Long> queue;
  39.         long sum;
  40.  
  41.         LimitedQueueWithSum(int sizeLimit) {
  42.             this.sizeLimit = sizeLimit;
  43.  
  44.             queue = new ArrayDeque<>();
  45.             sum = 0;
  46.         }
  47.  
  48.         void add(long value) {
  49.             queue.add(value);
  50.             sum = Problem2018.add(sum, value);
  51.  
  52.             if (queue.size() > sizeLimit) {
  53.                 long removed = queue.poll();
  54.                 sum = subtract(sum, removed);
  55.             }
  56.         }
  57.  
  58.         long getSum() {
  59.             return sum;
  60.         }
  61.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement