CSUOJ 1895 Apache is late again

Description

Apache is a student of CSU. There is a math class every Sunday morning, but he is a very hard man who learns late every night. Unfortunate, he was late for maths on Monday. Last week the math teacher gave a question to let him answer as a punishment, but he was easily resolved. So the math teacher prepared a problem for him to solve. Although Apache is very smart, but also was stumped. So he wants to ask you to solve the problem. Questions are as follows:You can find a m made (1 + sqrt (2)) ^ n can be decomposed into sqrt (m) + sqrt (m-1), if you can output m% 100,000,007 otherwise output No.

Input

There are multiply cases.Each case is a line of n. (|n| <= 10 ^ 18)

Output

Line, if there is no such m output No, otherwise output m% 100,000,007.

Sample Input

2

Sample Output

9

Hint

题目大意:给一个n,判断是否存在m使 (1 + sqrt (2)) ^ n= sqrt (m) + sqrt (m-1)
先列前面几项:
(1 + sqrt (2)) ^1 =sqrt (1*1+1) + sqrt (1)
(1 + sqrt (2)) ^ 2=sqrt (3*3) + sqrt (8)
(1 + sqrt (2)) ^ 3=sqrt (7*7+1) + sqrt (49)
(1 + sqrt (2)) ^ 4=sqrt (17*17) + sqrt (288)
...

an = 2 * an-1 +an-2
可以推出当n为奇数时m=an*an+1
n为偶数时 m=an*an

an可以用矩阵来表示

[an an-1]T =[ (2 1) (1 0)]*[an-2 an-1]T=[(2 1) (1 0)]^n-2 *[a2 a1]

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int MOD = 100000007;
typedef long long ll;
struct matrix{
	ll v[2][2];
	matrix()
	{
		memset(v, 0, sizeof(v));
	}
	matrix operator*(const matrix &m)
	{
		matrix c;
		for (int i = 0; i < 2; i++)
		{
			for (int j = 0; j < 2; j++)
			{
				for (int k = 0; k < 2; k++)
				{
					c.v[i][j] += (v[i][k] * m.v[k][j]) % MOD;
				}
			}
		}
		return c;
	}
};
matrix E, M, ans;
void init()
{
	for (int i = 0; i < 2; i++)
		E.v[i][i] = 1;
	M.v[0][0] = 2; M.v[0][1] = 1;
	M.v[1][0] = 1; M.v[1][1] = 0;
}
matrix quick_pow(matrix x, ll y)
{
	matrix tmp = E;
	while (y)
	{
		if (y & 1)
		{
			tmp =tmp* x;
			y--;
		}
		y >>= 1;
		x = x*x;
	}
	return tmp;
}
int main()
{
	ll n;
	init();
	while (~scanf("%lld", &n))
	{
		if (n < 0)
			printf("No\n");
		else if (n == 0)
			printf("1\n");
		else if (n == 1)
			printf("2\n");
		else if (n == 2)
			printf("9\n");
		else
		{
			ans = quick_pow(M, n - 2);
			ll a = (ans.v[0][0] * 3 + ans.v[0][1]) % MOD;
			if (n & 1)
				printf("%lld\n", ((a*a)%MOD + 1) % MOD);
			else
				printf("%lld\n", (a*a)%MOD);
		}
	}
	return 0;
}
/**********************************************************************
	Problem: 1895
	User: leo6033
	Language: C++
	Result: AC
	Time:8 ms
	Memory:2024 kb
**********************************************************************/






















转载于:https://www.cnblogs.com/csu-lmw/p/9124436.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值