Codeforces450 B. Jzzhu and Sequences (找规律) 题目链接:https://vjudge.net/problem/CodeForces-450B Input Output Input Output Input Output Note 解题思路: AC代码:
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).
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).
解题思路:
由数学公式f2 = f1 + f3输出任意 fn,看似简单,可是由于数据范围为2e9,所以明显是道矩阵快速幂,可是写的时候推导了前8项,发现每6个一个循环节,于是有了一种更简单的写法,具体看代码吧!
AC代码:
#include <stdio.h> #include <math.h> const long long MOD=1e9+7; int main() { int a[10],n; while(~scanf("%d %d",&a[1],&a[2])) { scanf("%d",&n); for(int i=3;i<=6;i++) a[i]=a[i-1]-a[i-2]; a[0]=a[6]; int ans; ans=( a[n%6]%MOD + MOD ) %MOD; printf("%d ",ans); } return 0; }