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
**********************************************************************/





CSUOJ 1895 Apache is late again的更多相关文章

  1. Why Apache Beam? A data Artisans perspective

    https://cloud.google.com/dataflow/blog/dataflow-beam-and-spark-comparison https://github.com/apache/ ...

  2. Intra-cluster Replication in Apache Kafka--reference

    Kafka is a distributed publish-subscribe messaging system. It was originally developed at LinkedIn a ...

  3. Apache Storm

    作者:jiangzz 电话:15652034180 微信:jiangzz_wx 微信公众账号:jiangzz_wy 背景介绍 流计算:将大规模流动数据在不断变化的运动过程中实现数据的实时分析,捕捉到可 ...

  4. 腾讯大数据平台Oceanus: A one-stop platform for real time stream processing powered by Apache Flink

    January 25, 2019Use Cases, Apache Flink The Big Data Team at Tencent     In recent years, the increa ...

  5. Apache Beam编程指南

    术语 Apache Beam:谷歌开源的统一批处理和流处理的编程模型和SDK. Beam: Apache Beam开源工程的简写 Beam SDK: Beam开发工具包 **Beam Java SDK ...

  6. Apache Beam: 下一代的大数据处理标准

    Apache Beam(原名Google DataFlow)是Google在2016年2月份贡献给Apache基金会的Apache孵化项目,被认为是继MapReduce,GFS和BigQuery等之后 ...

  7. Apache Spark 2.3.0 重要特性介绍

    文章标题 Introducing Apache Spark 2.3 Apache Spark 2.3 介绍 Now Available on Databricks Runtime 4.0 现在可以在D ...

  8. Apache和Nginx对比

    面试过程中被问到Apache和Nginx服务器的对比,因为之前没有关注过这个问题,所以也没能回答上来. 今天在网上搜索资料,发现中文资料极少,还是英文资料多一下. 原文链接:https://www.w ...

  9. org.apache.catalina.LifecycleException异常的处理

    今天调试了很久,重装tomcat都没用,后来发现是xml servlet的url-pattern的配置少写一个"/",添加后启动即可. org.apache.catalina.Li ...

随机推荐

  1. iscroll demo

    下面是自己找网上资料写的一个小demo,基础的属性和方法都有用到,但是用法还不是很标准,github上的demo用法很标准,外面一个wrapper,里面还得有一个scroller,如果要做跑马灯效果还 ...

  2. python 日期时间处理

    # 获取日期: import datetime #调用事件模块 today =datetime.date.today() #获取今天日期 deltadays =datetime.timedelta(d ...

  3. POJ 1128 Frame Stacking (拓扑排序)

    题目链接 Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ ...

  4. 字符加密 cipher

    评测传送门 Description: Valentino 函数的定义: 对于一个由数字和小写字母组成的字符串 S,两个整数 K,M,将 S 视为一个 P 进制数,定义: Valentino(S, K, ...

  5. 在Django中Session的那点事!

    1.session是什么 首先引入度娘的解释:Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及配置信息.这样,当用户在应用程序的 We ...

  6. Linux基础-yum软件包管理

    任务目标:自定义yum仓库:createrepo,自定义repo文件,使用yum命令安装httpd软件包,卸载httpd软件包:yum –y remove 软件名 ,使用yum安装组件'KDE 桌面' ...

  7. 差分约束系统+spfa(B - World Exhibition HDU - 3592 )

    题目链接:https://cn.vjudge.net/contest/276233#problem/B 思路和上一个一样,不过注意点有两个,第一,对dis数组进行初始化的时候,应该初始化成ox3f3f ...

  8. 未来人类T5 安装win10,ubuntu双系统

    1.首先确保win10已经安装,u盘中已刻录好系统,下载好英伟达最新驱动保存在u盘中,压缩100g的磁盘空间给ubuntu. 2.设置双显卡模式,重启时按F7选择进入u盘启动. 3.进入安装界面,选择 ...

  9. mysql中使用日期加减时无法识别年-月格式数据的问题,%Y-%m"这种格式数据

    最新做报表统计的时候处理按月统计部分时发现,虽然使用 DATE_FORMAT( time, '%Y-%m' ) 函数可以将日期格式转成年-月,但是如果是参数是年-月格式,即"2018-10& ...

  10. lombok java代码助手

    是不一个不错的代码生成工具,可以实现将代码更精简,且不失代码效率的一种不错的方法 https://www.cnblogs.com/qnight/p/8997493.html 通过java bean v ...