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. Git为某个域名设置代理

    打开Git 配置文件 vi ~/.gitconfig 添加如下配置: [http "https://github.com/"] proxy = http://127.0.0.1:1 ...

  2. windebug常用命令

    使用~查看所有线程 切换到一号线程:~1s 查看所有线程的托管堆栈  ~* e!clrstack 怎么查看,当前线程下,变量的信息? 对于托管代码而言,最核心的命令就是!do(dump object的 ...

  3. 零值比较--BOOL,int,float,指针变量与零值比较的if语句

    这是程序员面试的一道常见题,也是个C++基础问题.若只在大学里看过几本基础的编程入门书,看见这道题可能会觉得奇怪,不就是和0比较吗,直接拿出来比就是了,其实非也.下文引自google搜索结果,出处不详 ...

  4. 图文解说视频直播原理-zz

    本文主要介绍rtmp&hls视频直播原理,文章最早发表在我们的微信公众号上,详见这里,欢迎关注微信公众号blackerteam,更多详见www.blackerteam.com 现在视频直播很火 ...

  5. mysql条件查询中AND与OR联合使用的注意事项!

    mysql查询中经常会用到AND与OR一起使用的情况,可如果写法不对,往往会起到相反的效果,这不,前几天就碰到了,最后测试果然提了一堆bug!!!! 废话就不多说了,主要总结一下几点: 一 当mysq ...

  6. Exif xss

    这种XSS出现的状况会特别少. Exif是啥??? 可交换图像文件格式(英语:Exchangeable image file format,官方简称Exif),是专门为数码相机的照片设定的,可以记录数 ...

  7. sicily 1500. Prime Gap

    Description The sequence of n ? 1 consecutive composite numbers (positive integers that are not prim ...

  8. C/C++杂记:NULL与0的区别、nullptr的来历

    某些时候,我们需要将指针赋值为空指针,以防止野指针.   有人喜欢使用NULL作为空指针常量使用,例如:int* p = NULL;. 也有人直接使用0值作为空指针常量,例如:int* p = 0;. ...

  9. Pytorch多进程最佳实践

    预备知识 模型并行( model parallelism ):即把模型拆分放到不同的设备进行训练,分布式系统中的不同机器(GPU/CPU等)负责网络模型的不同部分 —— 例如,神经网络模型的不同网络层 ...

  10. vue+vuex+axios+echarts画一个动态更新的中国地图

    一. 生成项目及安装插件 # 安装vue-cli npm install vue-cli -g # 初始化项目 vue init webpack china-map # 切到目录下 cd china- ...