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

本文介绍了一种名为Jzzhu的序列,并提供了一个高效的算法来计算该序列中第n项的值,模10^9 + 7。序列定义为f[0]=x, f[1]=y, 并通过递推公式展开。文章给出了解决方案并通过代码实现,展示了如何处理大数值的计算及周期性的利用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 -1
2
output
1000000006
Note

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

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

题意很简单。

题解:f[0]=x,f[1]=y,f[2]=y-x,f[3]=-x,f[4]=-y,f[5]=x-y。然后开始重复了,节点为6.这里求余1e9+7有个问题是负号,即是1e9+7加上这个数。有可能还是有点问题,懒得看了。

#include <bits/stdc++.h>
using namespace std;
const int M=1e9+7;
__int64 f[10],n;
__int64 mod(__int64 x)
{
	if (x>=0) return x % M;
	else return M+x;
}
int main()
{
	scanf("%I64d%I64d",&f[0],&f[1]);
	//f[0]=mod(f[0]);
	//f[1]=mod(f[1]);
	f[2]=mod(f[1]-f[0]);
	f[3]=mod(0-f[0]);
	f[4]=mod(0-f[1]);
	f[5]=mod(f[0]-f[1]);
	scanf("%I64d",&n);
	printf("%d\n",mod(f[(n-1) % 6]));
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值