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;
}
}

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

  1. (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 al ...

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

    题目链接:http://codeforces.com/problemset/problem/450/B B. Jzzhu and Sequences time limit per test 1 sec ...

  3. CodeForces 450B Jzzhu and Sequences (矩阵优化)

    CodeForces 450B Jzzhu and Sequences (矩阵优化) Description Jzzhu has invented a kind of sequences, they ...

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

    B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...

  5. CF450B Jzzhu and Sequences(矩阵加速)

    CF450B Jzzhu and Sequences 大佬留言:这.这.不就是矩乘的模板吗,切掉它!! You are given xx and yy , please calculate $f_{n ...

  6. CodeForces - 450B Jzzhu and Sequences —— 斐波那契数、矩阵快速幂

    题目链接:https://vjudge.net/problem/CodeForces-450B B. Jzzhu and Sequences time limit per test 1 second ...

  7. codeforces 450B B. Jzzhu and Sequences(矩阵快速幂)

    题目链接: B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input ...

  8. Codeforces450 B. Jzzhu and Sequences

    B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...

  9. 数学 找规律 Jzzhu and Sequences

    A - Jzzhu and Sequences   Jzzhu has invented a kind of sequences, they meet the following property: ...

随机推荐

  1. Java JDBC 基础知识

    一.JDBC常用接口.类介绍 JDBC提供对独立于数据库统一的API,用以执行SQL命令.API常用的类.接口如下: DriverManager 管理JDBC驱动的服务类,主要通过它获取Connect ...

  2. Object类型转换为long或者Long

    1.转换为long Object o = new Object();long l = Long.valueOf(String.valueOf(o)).longValue(); 2.转换为Long Ob ...

  3. Rational Rose、PowerDesign、Visio的一些比较

    就目前建模软件来说,Rational Rose.PowerDesign.Visio三个是比较常用的系列了,在这里对它们做一些比较,我只用过PowerDesign.Visio和一个跟Rose很像的免费工 ...

  4. 5.synchronized锁重入

    package demo1; /** * synchronized锁重入 * Created by liudan on 2017/6/5. */ public class MyThread5_sync ...

  5. poj 食物链

    比基础的并查集有些进步. 在以下这个链接中有详解: http://blog.csdn.net/ditian1027/article/details/20804911 对于每两个动物的关系,都是先推与终 ...

  6. 点云数据(point cloud) 【转】

    转自caimagic的专栏 一:什么是点云数据 点云数据是指在一个三维坐标系统中的一组向量的集合.这些向量通常以X,Y,Z三维坐标的形式表示,而且一般主要用来代表一个物体的外表面形状.不经如此,除(X ...

  7. Android中的树状(tree)列表

    树状列表前端挺常用的,还有人专门写过Ztree,Android中有的时候也需要使用到树状列表,上篇文章写了一下ExpandableListView,ExpandableListView最多支持两级结构 ...

  8. Bridge 桥接模式 MD

    桥接模式 简介 将抽象部分与实现部分分离,使它们都可以独立的变化. 业务抽象角色引用业务实现角色,或者说业务抽象角色的部分实现是由业务实现角色完成的 Bridge模式基于类的最小设计原则,通过使用封装 ...

  9. Kettle中调用用户自定义的jar包

    ETL工具断断续续的也接触了 Informatica,Kettle, SSIS,个人感觉Info很强大但是也很贵,而且有着一些神秘感.Kettle 4.0版本以来已经有了User  defined j ...

  10. docker入门——构建镜像

    前面我们已经介绍了如何拉取已经构建好的带有定制内容的Docker镜像,那么如何构建自己的镜像呢? 构建Docker镜像有以下两种方法: 使用docker commit命令. 使用docker buil ...