DMOPC '20 Contest 3 P3 - A Ring of Buckets

View as PDF

Submit solution


Points: 12 (partial)
Time limit: 2.0s
Memory limit: 256M

Author:
Problem types

Lily has a ring of N buckets, numbered from 1 to N. Each bucket has capacity M. She has a pouring bucket with capacity K+2, and wants to fill all buckets completely without any overflow, that is, to M exactly. Unfortunately, every time she tries to pour into a bucket, she spills a little, and 1 unit is spilled into each adjacent bucket, with K poured into her original bucket. Note that 1 and N are adjacent.

Lily wants to fill all of the buckets to capacity, but wants to have zero overflow. Being a genius, Lily already knows the answer, and challenges you to find it too.

However, Lily will quickly bore of you listing out the N numbers, so she decides on the following formula. If Ai is the number of times you must pour into the ith bucket, Lily will choose an arbitrary number B and ask you to compute A1+A2×B+A3×B2++AN×BN1. This number may be large, so Lily will be satisfied if you can output it modulo 109+7. Additionally, if there are multiple solutions, choose the one that has the lexicographically smallest value of A1,A2,,AN.

Finally, Lily has a lot of buckets, so she will ask you Q questions, each with their own values of N,M,K and B. Can you answer them all?

Constraints

For all subtasks:

1M,K109

2B109

3N109

1Q104

Subtask 1 [1/15]

Q=1

N,M,K5

Subtask 2 [2/15]

Q=1

N,M,K400

Subtask 3 [3/15]

Q=1

K=1

1N,M106

Subtask 4 [3/15]

Q=1

K=2

1N,M106

Subtask 5 [3/15]

Q=1

3K106

1N,M106

Subtask 6 [3/15]

No additional constraints.

Input Specification

The first line will contain Q, the number of questions.

The next Q lines will contain four integers each, N,M,K,B.

Output Specification

Output Q lines. For each question, if it is impossible to fill all the buckets exactly to capacity, output -1.

Otherwise, output the required integer as specified above. Don't forget to output it modulo 109+7.

Sample Input 1

Copy
1
4 4 1 100

Sample Output 1

Copy
-1

Explanation for Sample Output 1

There is no way to fill all the buckets exactly.

Sample Input 2

Copy
1
3 4 2 7

Sample Output 2

Copy
57

Explanation for Sample Output 2

If we pour once into each bucket, we get a solution array 1 1 1. Then, our required value is:

1+1×7+1×72=57

Sample Input 3

Copy
1
999999 999999 5 8

Sample Output 3

Copy
35952588

Comments


  • 0
    d3story  commented on Feb. 7, 2021, 11:48 p.m. edit 2

    Can someone please take a look at my code I spent a lot of time but I can't seem to find what is wrong? https://dmoj.ca/src/3377250


    Ps I took a look at the editorial I think it has something to do with the big numbers. I think my logic is correct


    • 1
      Kirito  commented on Feb. 8, 2021, 12:26 a.m.

      Hint: compute 230, and then take it modulo 109+7.

      Note that this number is not even. What does tell you about naively combining division and modulo operations?


      • 0
        d3story  commented on Feb. 8, 2021, 3:07 a.m. edit 2

        Is there any good way to overcome this though, I tried to use the Modular inverse thing to find a solution But I am quite sure it doesn't work 100% of the time.


        btw tysm for helping