CSU 1895 Apache is late again(根号2加1的n次方)
来源:互联网 发布:css布局 书籍 知乎 编辑:程序博客网 时间:2024/06/04 18:24
题目:
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
这个题目主要是代数运算,有2类方法,一类是一元递推,一类是二元递推
我用的是一元递推的方法,如下:
接下来就直接根据这个式子求出m就可以了。
不过因为n很大,所以肯定不是用组合数,而是用递推。
虽然我们要求的是bn,但是bn是非齐次线性递推,没法直接矩阵快速幂,而an是齐次线性递推,可以用矩阵快速幂。
需要注意的是从an转化到bn需要除以4,所以需要用到逆元
这样,答案便出来了。
代码:
#include<iostream>using namespace std;long long l[4]={6,100000006,1,0},l1[4],ans[4],p=100000007,p11=50000004;void g(long long *p1,long long *p2,long long *p3){p3[0]=(p1[0]*p2[0]+p1[1]*p2[2])%p;p3[1]=(p1[0]*p2[1]+p1[1]*p2[3])%p;p3[2]=(p1[2]*p2[0]+p1[3]*p2[2])%p;p3[3]=(p1[2]*p2[1]+p1[3]*p2[3])%p;}void f(long long n){if(n==0){ans[0]=ans[3]=1,ans[1]=ans[2]=0;return;}f(n/2);for(int i=0;i<4;i++)l1[i]=ans[i];g(l1,l1,ans);if(n%2==0)return;for(int i=0;i<4;i++)l1[i]=ans[i];g(l1,l,ans);}int main(){long long n,k;while(cin>>n){if(n>0){f(n-1);k=(ans[0]*6+ans[1]*2)%p;cout<<((k*p11)%p+1)*p11%p<<endl;}if(n==0)cout<<1<<endl;if(n<0)cout<<"No\n";}return 0;}
如果用二元递推的话会更简单。
- CSU 1895 Apache is late again(根号2加1的n次方)
- 中南大学第十一届大学生程序设计竞赛-COJ1895-Apache is late again
- 计算 1+2(2次方)+3(3次方)+...+n(n次方)的值
- 2的N次方
- 2的N次方
- 2的N次方
- 2的n次方
- 2的n次方
- 2的n次方
- 数值的整数次方 和开根号
- 试比较2的n次方加2与n平方的大小。 (n属于N*). 用数学规纳发证明
- 【hdu 1787】 GCD Again(根号n求phi)
- 【高精度】2的n次方
- ACM 2的N次方
- 计算2的n次方
- 计算2的N次方
- 2^N-2的N次方
- N的N次方
- 深度学习框架Keras安装指南
- git简单推送到远程仓库流程
- 微信小程序
- Leetcode 22. Generate Parentheses
- IOS开发之HomeKit(四)
- CSU 1895 Apache is late again(根号2加1的n次方)
- 原生JS添加鼠标事件
- 数字的大写字母的乱序整理
- Android Native嵌入React Native模块
- uva401Palindromes(回文串和镜像串的判断)
- 图片处理,圆形圆角描边
- AngularJS之使用服务封装
- Vim快速选中、删除、复制引号或括号中的内容
- flume+kafka+hdfs日志系统