练习cf450B. Jzzhu and Sequences
题目如下
B. Jzzhu and Sequences
time limit per test1 second
memory limit per test256 megabytes
Jzzhu has invented a kind of sequences, they meet the following property:

You are given x and y, please calculate fn modulo 1000000007 (109 + 7).
Input
The first line contains two integers x and y (|x|, |y| ≤ 109). The second line contains a single integer n (1 ≤ n ≤ 2·109).
Output
Output a single integer representing fn modulo 1000000007 (109 + 7).
题目大意
根据fi = fi-1 + fi+1 ,对fn进行取模并输出。
题目分析
根据fi = fi-1 + fi+1 推出 fi = fi-1 - fi-2(i>=3),从后往前推发现存在周期为6的循环,那么
点击查看代码
#include <iostream>
using namespace std;
int main(){
long long x, y, n;
cin >> x >> y;
cin >> n;
long long ans, res;
long long k= (n - 1) % 6;
if(k == 0){
ans = x;
}else if(k == 1){
ans = y;
}else if(k == 2){
ans = y - x;
}else if (k == 3){
ans = -x;
}else if (k == 4){
ans = -y;
}else{
ans = x - y;
}
res = ans % 1000000007;
if(res < 0){
res += 1000000007;
}
cout << res << "";
return 0;
}

浙公网安备 33010602011771号