您的位置:首页 > 产品设计 > UI/UE

Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences

2014-07-20 01:03 330 查看
B. Jzzhu and Sequences

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

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).

Sample test(s)

input
2 3
3


output
1


input
0 -12


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 [ i ] = f [ i-1 ] +f [ i + 1];

同时给定了f [ 1 ] 和 f [ 2 ];

让我们求f [ n ] mod 10^9+7 的值,其中如果f [ n ] 小于0 则对 算模n加的值,即 (10^9+7+f [ n ])mod(10^9 + 7);

好了,题目大意已知,接下来就是计算的问题了;

首先,如果说不是看到N的取值为 2 * 10^ 9 的话我是果断会暴力解决的。- -!

所以,常规的暴力方法就直接PASS掉吧。。。

思路: 既然暴力不行了,那就只能从项数公式入手解决了。。

仔细观察后咱会发现。。。。

如果把公式 f [ i ] = f [ i-1 ] +f [ i + 1];变为 f [ i ] = f [ i- 1 ]+f [ i - 2 ];的话。。。

得:

f[1]=f1;
    f[2]=f2;
    f[3]=f[2]-f[1]=f2-f1;
    f[4]=f[3]-f[2]=-f1;
    f[5]=f[4]-f[3]=-f2;
    f[6]=f[5]-f[4]=-f2+f1;
    f[7]=f[6]-f[5]=f1;


可以看出,它的是以7为循环的数,所以就可以很容易的写出代码了,代码如下:

#include <iostream>
using namespace std;

const int maxx=1000000007;

int main( )
{
    int f[7];
    int f1,f2;
    cin>>f1>>f2;
    f[1]=f1;
    f[2]=f2;
    f[3]=f2-f1;
    f[4]=-f1;
    f[5]=-f2;
    f[0]=-f2+f1;
    int n;
    cin>>n;
    n=n%6;
    f
=f
%maxx;
    if(f
<0)
    {
        cout<<maxx+f
<<endl;
    }
    else
    {
        cout<<f
<<endl;
    }
    return 0;
}


如发现bug,欢迎指出,万分感谢!!

以上。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: