您的位置:首页 > 其它

2013 ACM-ICPC长沙赛区全国邀请赛So Easy! && 2015 ACM/ICPC Asia Regional Shenyang Online-Best Solver

2015-10-02 11:00 585 查看


<span style="background-color: transparent; color: rgb(124, 169, 237); font-family: Arial; font-size: 18px;">So Easy!</span>

  A sequence Sn is defined as:



Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example, ┌3.14┐=4. You are to calculate Sn.

  You, a top coder, say: So easy!



Input

  There are several test cases, each test case in one line contains four positive integers: a, b, n, m. Where 0< a, m < 215, (a-1)2< b < a2, 0 < b, n < 231.The input will finish with the end of file.

Output

  For each the case, output an integer Sn.

Sample Input

2 3 1 2013
2 3 2 2013
2 2 1 2013


Sample Output

4
14
4




4

那么用矩阵快速幂就可以了,代码如下:


Best Solver

Problem Description

The so-called best problem solver can easily solve this problem, with his/her childhood sweetheart.

It is known that y=(5+26√)1+2x.

For a given integer x (0≤x<232) and
a given prime number M (M≤46337),
print [y]%M.
([y] means
the integer part of y)

Input

An integer T (1<T≤1000),
indicating there are T test
cases.

Following are T lines,
each containing two integers x and M,
as introduced above.

Output

The output contains exactly T lines.

Each line contains an integer representing [y]%M.

Sample Input

7
0 46337
1 46337
3 46337
1 46337
21 46337
321 46337
4321 46337


Sample Output

Case #1: 97
Case #2: 969
Case #3: 16537
Case #4: 969
Case #5: 40453
Case #6: 10211
Case #7: 17947


此题的a、b完全满足上题的要求,可用与上题完全相同的思路求解,唯一的不同点即为时向下取整而非向上取整,那么,在上题的答案基础上,减一,就是此题的答案。

  还有就是指数太大,要找到循环节,其实由于mod小,循环节并没有太大,是可以搞的。

代码如下:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: