codeforces-DIV2-B- Jzzhu and Sequences

简介:

 Jzzhu and Sequences

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

InputThe first line contains two integers x and y (|x|, |y| ≤ 109). The second line contains a single integer n (1 ≤ n ≤ 2·109).

OutputOutput a single integer representing fn modulo 1000000007 (109 + 7).

Sample test(s)input

2 3
3

output

1

input

0 -1
2

output

1000000006

NoteIn the first sample, f2 = f1 + f33 = 2 + f3f3 = 1.

In the second sample, f2 =  - 1 - 1 modulo (109 + 7) equals (109 + 6).

 

 

斐波那契数列+负数取余+找规律

AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
    int i,j,n,m,f1,f2,f3;
    scanf("%d %d",&f1,&f2);
    scanf("%d",&n);
    if(n<3)
    {
       if(n==1)
       {
           if(f1<0)
           printf("%d\n",f1+1000000007);
           else
           printf("%d\n",f1%1000000007);
       }
       else
       {
           if(f2<0)
           printf("%d\n",f2+1000000007);
           else
           printf("%d\n",f2%1000000007);
       }
    }
    else
    {
        n=(n-3)%6+3;
        for(j=3;j<=n;j++)
        {
           f3=(f2-f1);
           if(f2<0)
           {
              f2=f2+1000000007; 
           }
           else
           f2=f2%1000000007;
           f1=f2;
           if(f3<0)
           {
              f3=f3+1000000007;    
           }
           else
           f3=f3%1000000007;
           f2=f3;
        }
        printf("%d\n",f3%1000000007);  
    }
    return 0;
}
 

相关文章
|
9月前
Codeforces Round #192 (Div. 2) (330B) B.Road Construction
要将N个城市全部相连,刚开始以为是最小生成树的问题,其实就是一道简单的题目。 要求两个城市之间不超过两条道路,那么所有的城市应该是连在一个点上的,至于这个点就很好找了,只要找到一个没有和其他点有道路限制的即可。
22 0
|
10月前
|
人工智能 算法
Codeforces #737Div2A. A. Ezzat and Two Subsequences(模拟)
Codeforces #737Div2A. A. Ezzat and Two Subsequences(模拟)
30 0
|
机器学习/深度学习 人工智能 BI
Educational Codeforces Round 115 (Rated for Div. 2) D. Training Session(组合数学 思维)
Educational Codeforces Round 115 (Rated for Div. 2) D. Training Session(组合数学 思维)
90 0
|
机器学习/深度学习 Java
codeforces Educational Codeforces Round 49 (Rated for Div. 2) C题
刚开始拿到这题很懵逼,知道了别人的思路之后开始写,但是还是遇到很多坑,要求求P2/S最大。p=a b。就是求(a2+ b2 +2ab)/ab最大,也就是a/b +b/a最大。那么题意就很明显了。
93 0