CodeForces - 450B Jzzhu and Sequences
作者:互联网
题目描述:
Jzzhu has invented a kind of sequences, they meet the following property:
You are given x and y, please calculate modulo 1000000007 ( + 7).
Input
The first line contains two integers x and y (|x|, |y| ≤ ). The second line contains a single integer n (1 ≤ n ≤ ).
Output
Output a single integer representing modulo 1000000007 ( + 7).
Examples
Input
2 3
3
Output
1
Input
0 -1
2
Output
1000000006
Note
In the first sample, , 3 = 2 + , = 1.
In the second sample, = - 1; - 1 modulo ( + 7) equals ( + 6).
解题报告:
1:将所有取模换为 (x%mod+mod)%mod,接下来就是套模板了。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 2;
const ll mod = 1000000007;
struct matrix{
ll num[N][N];
}A, B;
void init(){
memset(A.num, 0, sizeof(A.num));
memset(B.num, 0, sizeof(B.num));
A.num[0][0] = A.num[1][0] = 1;A.num[0][1] = -1;
}
matrix mul(matrix AA, matrix BB){
matrix C;
memset(C.num, 0, sizeof(C.num));
for(ll i=0; i<N; ++i){
for(ll j=0; j<N; ++j){
for(ll k=0; k<N; ++k){
C.num[i][j] += AA.num[i][k]*BB.num[k][j];
C.num[i][j] = (C.num[i][j]%mod+mod)%mod;
}
}
}
return C;
}
ll Pow(ll n, ll x, ll y){
B.num[0][0] = y%mod, B.num[1][0] = x%mod;
while(n){
if(n&1)B = mul(A, B);
n >>= 1, A = mul(A, A);
}
return (B.num[0][0]%mod+mod)%mod;
}
int main(){
ll n, x, y;
scanf("%lld%lld%lld", &x, &y, &n);
init();
if(n == 1){
printf("%lld\n", (x%mod+mod)%mod);
return 0;
}
if(n == 0){
printf("%lld\n", ((x-y)%mod+mod)%mod);
return 0;
}
printf("%lld\n", Pow(n-2, x, y));
return 0;
}
baronLJ 发布了149 篇原创文章 · 获赞 4 · 访问量 1万+ 私信 关注
标签:matrix,ll,CodeForces,Jzzhu,num,Sequences,Output,lld,mod 来源: https://blog.csdn.net/jun_____/article/details/104072824