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. java 连接Kafka报错java.nio.channels.ClosedChannelExcep

    Java 客户端连接Kafka报如下错误 java.nio.channels.ClosedChannelExcep 是由于Kafka server.properties中的advertised.hos ...

  2. 使用win10 hyper-v安装linux系统

    1.控制面板---程序---启动或关闭windows功能---启动hyper-v管理器---重启 2.配置网络 因为公司内网通过ip验证,而通过桥接的方式,虚拟机就相当于物理机所在的网络中的一台真实主 ...

  3. Ubuntu 14.04 Nvidia显卡驱动安装及设置

    更换主板修复grub 引导后,无法从Nvidia进入系统(光标闪烁), 可能是显卡驱动出了问题. 1. 进入BIOS设置, 从集成显卡进入系统 将显示器连接到集显的VGI口, 并在BIOS中设置用集显 ...

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

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

  5. jq 判断鼠标滚动上下

    $(document).on("mousewheel DOMMouseScroll", function (e) { var delta = (e.originalEvent.wh ...

  6. python基础===一行 Python 代码实现并行(转)

    原文:https://medium.com/building-things-on-the-internet/40e9b2b36148 译文:https://segmentfault.com/a/119 ...

  7. 25 个常用的 Linux iptables 规则【转】

    转自 25 个常用的 Linux iptables 规则 - 文章 - 伯乐在线http://blog.jobbole.com/108468/ # 1. 删除所有现有规则 iptables -F # ...

  8. js选择checkbox值,组织成key-value形式,传值到后台

    最近项目中遇到这样一个问题,接口定义需要传一个Map<String,String[]> params的参数,需要在jsp页面组织数据到后台操作,所以记下来以后难免还会用到. 以下是java ...

  9. URL的井号

    2010年9月,twitter改版. 一个显著变化,就是URL加入了"#!"符号.比如,改版前的用户主页网址为 http://twitter.com/username 改版后,就变 ...

  10. java基础55 UDP通讯协议和TCP通讯协议

    本文知识点(目录): 1.概述    2.UDP通讯协议    3.TCPP通讯协议 1.概述 1.在java中网络通讯作为Socket(插座)通讯,要求两台都必须安装socket.    2.不同的 ...