Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private final static long MODULO = 1000 * 1000 * 1000 + 7;
- private void solve() {
- int n = readInt();
- int a = readInt();
- int b = readInt();
- LimitedQueueWithSum aQueue = new LimitedQueueWithSum(a);
- LimitedQueueWithSum bQueue = new LimitedQueueWithSum(b);
- aQueue.add(1);
- bQueue.add(1);
- for (int length = 2; length <= n; ++length) {
- long aSum = aQueue.getSum();
- long bSum = bQueue.getSum();
- aQueue.add(bSum);
- bQueue.add(aSum);
- }
- long answer = add(aQueue.getSum(), bQueue.getSum());
- out.println(answer);
- }
- static long add(long a, long b) {
- return (a + b) % MODULO;
- }
- static long subtract(long a, long b) {
- return add(a, MODULO - b % MODULO);
- }
- static class LimitedQueueWithSum {
- final int sizeLimit;
- Queue<Long> queue;
- long sum;
- LimitedQueueWithSum(int sizeLimit) {
- this.sizeLimit = sizeLimit;
- queue = new ArrayDeque<>();
- sum = 0;
- }
- void add(long value) {
- queue.add(value);
- sum = Problem2018.add(sum, value);
- if (queue.size() > sizeLimit) {
- long removed = queue.poll();
- sum = subtract(sum, removed);
- }
- }
- long getSum() {
- return sum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement