B. Jzzhu and Sequences
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard outputJzzhu 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).
Sample test(s)
input
2 3 3
output
1
input
0 -1 2
output
1000000006
Note
In the first sample, f2 = f1 + f3, 3 = 2 + f3, f3 = 1.
In the second sample, f2 = - 1; - 1 modulo (109 + 7) equals (109 + 6).
题意很简单。
题解:f[0]=x,f[1]=y,f[2]=y-x,f[3]=-x,f[4]=-y,f[5]=x-y。然后开始重复了,节点为6.这里求余1e9+7有个问题是负号,即是1e9+7加上这个数。有可能还是有点问题,懒得看了。
#include <bits/stdc++.h>
using namespace std;
const int M=1e9+7;
__int64 f[10],n;
__int64 mod(__int64 x)
{
if (x>=0) return x % M;
else return M+x;
}
int main()
{
scanf("%I64d%I64d",&f[0],&f[1]);
//f[0]=mod(f[0]);
//f[1]=mod(f[1]);
f[2]=mod(f[1]-f[0]);
f[3]=mod(0-f[0]);
f[4]=mod(0-f[1]);
f[5]=mod(f[0]-f[1]);
scanf("%I64d",&n);
printf("%d\n",mod(f[(n-1) % 6]));
return 0;
}

本文介绍了一种名为Jzzhu的序列,并提供了一个高效的算法来计算该序列中第n项的值,模10^9 + 7。序列定义为f[0]=x, f[1]=y, 并通过递推公式展开。文章给出了解决方案并通过代码实现,展示了如何处理大数值的计算及周期性的利用。
1497

被折叠的 条评论
为什么被折叠?



