最难的是要想到无理数共轭...
参考题解:点击打开链接
还要注意注意负数的取模,可以先+mod再进行取模.
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a, b, m;
struct node
{
ll s[2][2];
node() {}
node(ll a, ll b, ll c, ll d)
{
s[0][0] = a;
s[0][1] = b;
s[1][0] = c;
s[1][1] = d;
}
};
node mul(node a, node b)
{
node t = node(0, 0, 0, 0);
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
for(int k = 0; k < 2; k++)
t.s[i][j] = (t.s[i][j]+a.s[i][k]*b.s[k][j]+m)%m; //注意负数
return t;
}
node mt_pow(node p, int n)
{
node q = node(1, 0, 0, 1);
while(n)
{
if(n&1) q = mul(p, q);
p = mul(p, p);
n /= 2;
}
return q;
}
int main(void)
{
ll n;
while(cin >> a >> b >> n >> m)
{
node base = node(2*a, (b-a*a), 1, 0);
ll c1 = 2*a%m;
ll c2 = 2*(a*a+b)%m;
if(n == 1) printf("%lld\n", c1);
else if(n == 2) printf("%lld\n", c2);
else
{
node ans = mt_pow(base, n-2);
printf("%lld\n", (ans.s[0][0]*c2+ans.s[0][1]*c1+m)%m);
}
}
return 0;
}
So Easy!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3883 Accepted Submission(s): 1279
Problem Description
A sequence S
n 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 S n.
You, a top coder, say: So easy!
Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example, ┌3.14┐=4. You are to calculate S n.
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 < 2
15, (a-1)
2< b < a
2, 0 < b, n < 2
31.The input will finish with the end of file.
Output
For each the case, output an integer S
n.
Sample Input
2 3 1 2013 2 3 2 2013 2 2 1 2013
Sample Output
4 14 4
Source