(CF#257)B. 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).

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

本来9点的CF,今天有学姐来,讲到了9点半,这题最后没注意坑点,最后判的时候还wa了,掉了100分,蛋疼中

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1100;
const int M=1000000007;
int a[maxn];
int main()
{
   int x,y,n;
   while(cin>>x>>y>>n)
   {
       a[1]=x;
       a[2]=y;
       int len=0,t;
       for(int i=3;;i++)
       {
           a[i]=a[i-1]-a[i-2];
           if(a[i]==a[2]&&a[i-1]==a[1]&&i>=4)
           {
               len=i-2;
               break;
           }
           if(i>=n)
             break;
       }
       if(len)
       {
//          cout<<"len:"<<len<<endl;
          t=(n-1)%len+1;
       }
       else
          t=n;
       if(a[t]>0)
          cout<<a[t]%M<<endl;
       else
       {
           while(a[t]<0)
             a[t]+=M;
           cout<<a[t]%M<<endl;
       }
   }
   return 0;
}

看了别人的想法,我的还是太狭隘了。我仅仅知道找规律,别人找的规律更详细。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int M=(1e9)+7;
int a[6];

int main()
{
    int x,y,n;
    while(cin>>x>>y>>n)
    {
        a[1]=(x+M)%M;
        a[2]=(y+M)%M;
        a[3]=(a[2]-a[1]+M)%M;
        a[4]=(-x+M)%M;
        a[5]=(-y+M)%M;
        a[0]=(a[1]-a[2]+M)%M;
        cout<<(a[n%6]+M)%M<<endl;
    }
}
时间: 2024-10-15 16:57:59

(CF#257)B. Jzzhu and Sequences的相关文章

(CF#257)C. Jzzhu and Chocolate

Jzzhu has a big rectangular chocolate bar that consists of n?×?m unit squares. He wants to cut this bar exactly k times. Each cut must meet the following requirements: each cut should be straight (horizontal or vertical); each cut should go along edg

(CF#257)A. Jzzhu and Children

There are n children in Jzzhu's school. Jzzhu is going to give some candies to them. Let's number all the children from 1 to n. The i-th child wants to get at least ai candies. Jzzhu asks children to line up. Initially, the i-th child stands at the i

【矩阵快速幂 】Codeforces 450B - Jzzhu and Sequences (公式转化)

[题目链接]click here~~ [题目大意] Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, please calculate fn modulo1000000007(109?+?7). [解题思路] /*A - Jzzhu and Sequences Codeforces 450B - Jzzhu and Sequences ( 矩阵快速幂 )

Codeforces Round #258 (Div. 2) B. Jzzhu and Sequences(矩阵快速幂)

题目链接:http://codeforces.com/problemset/problem/450/B ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943

【机器学习算法-python实现】协同过滤(cf)的三种方法实现

(转载请注明出处:http://blog.csdn.net/buptgshengod) 1.背景 协同过滤(collaborative filtering)是推荐系统常用的一种方法.cf的主要思想就是找出物品相似度高的归为一类进行推荐.cf又分为icf和ucf.icf指的是item collaborative filtering,是将商品进行分析推荐.同理ucf的u指的是user,他是找出知趣相似的人,进行推荐.通常来讲icf的准确率可能会高一些,通过这次参加天猫大数据比赛,我觉得只有在数据量非

Codeforces Round 450 Div2 B.Jzzhu and Sequences

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: ![](https://codeforces.com/predownloaded/55/

Codeforces Round #257 (Div. 2) B 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). Input The first line contains two integers x and y (|x|,?|y|?≤?109). The second line contains a single i

Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences (矩阵快速幂)

题目链接:http://codeforces.com/problemset/problem/450/B 题意很好懂,矩阵快速幂模版题. 1 /* 2 | 1, -1 | | fn | 3 | 1, 0 | | fn-1 | 4 */ 5 #include <iostream> 6 #include <cstdio> 7 #include <cstring> 8 using namespace std; 9 typedef __int64 LL; 10 LL mod =

Codeforces 450B div.2 Jzzhu and Sequences 矩阵快速幂or规律

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 i