题目链接:

1537 分解

 问(1+sqrt(2)) ^n  能否分解成 sqrt(m) +sqrt(m-1)的形式 
如果可以 输出 m%1e9+7 否则 输出no
Input
一行,一个数n。(n<=10^18)
Output
一行,如果不存在m输出no,否则输出m%1e9+7
Input示例
2
Output示例
9

题意:

思路:

发现跟奇数偶数有关系,然后就找出递推式,然后就快速幂,然后就A了;

AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=5e5+20;
const int maxn=1e4+220;
const double eps=1e-12; struct matrix
{
LL a[2][2];
}; matrix cal(matrix A,matrix B)
{
matrix C;
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
C.a[i][j]=0;
for(int k=0;k<2;k++)
{
C.a[i][j]=(C.a[i][j]+A.a[i][k]*B.a[k][j])%mod;
}
}
}
return C;
}
matrix pow_mod(LL x)
{
matrix s,base;
base.a[0][0]=base.a[1][1]=base.a[1][0]=1;base.a[0][1]=2;
s.a[0][0]=s.a[1][1]=1;s.a[0][1]=s.a[1][0]=0;
while(x)
{
if(x&1)s=cal(s,base);
base=cal(base,base);
x>>=1;
}
return s;
}
int main()
{
LL n,ans=0;
read(n);
if(n<0)cout<<"no\n";
else if(n==0)cout<<"1\n";
else {
matrix temp=pow_mod(n-1);
if(n%2==0)
{
ans=(temp.a[0][0]+temp.a[0][1])%mod;
ans=ans*ans%mod;
}
else
{
//cout<<temp.a[1][0]<<t
ans=(temp.a[1][0]+temp.a[1][1])%mod;
ans=ans*ans%mod*2%mod;
}
cout<<ans<<endl;
}
return 0;
}

  


51nod-1537 1537 分解(矩阵快速幂+找规律)的更多相关文章

  1. hdu 1005 Number Sequence(矩阵快速幂,找规律,模版更通用)

    题目 第一次做是看了大牛的找规律结果,如下: //显然我看了答案,循环节点是48,但是为什么是48,据说是高手打表出来的 #include<stdio.h> int main() { ], ...

  2. Codeforces 450B div.2 Jzzhu and Sequences 矩阵快速幂or规律

    Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...

  3. 【BZOJ2432】【NOI2011】兔农(数论,矩阵快速幂)

    [BZOJ2432][NOI2011]兔农(数论,矩阵快速幂) 题面 BZOJ 题解 这题\(75\)分就是送的,我什么都不想写. 先手玩一下,发现每次每次出现\(mod\ K=1\)的数之后 把它减 ...

  4. 51nod 1113 矩阵快速幂

    题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...

  5. A - Number Sequence(矩阵快速幂或者找周期)

    Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * ...

  6. 2017ACM暑期多校联合训练 - Team 2 1006 HDU 6050 Funny Function (找规律 矩阵快速幂)

    题目链接 Problem Description Function Fx,ysatisfies: For given integers N and M,calculate Fm,1 modulo 1e ...

  7. hdu 2604 Queuing dp找规律 然后矩阵快速幂。坑!!

    http://acm.hdu.edu.cn/showproblem.php?pid=2604 这题居然O(9 * L)的dp过不了,TLE,  更重要的是找出规律后,O(n)递推也过不了,TLE,一定 ...

  8. Nowcoder 练习赛 17 C 操作数 ( k次前缀和、矩阵快速幂打表找规律、组合数 )

    题目链接 题意 :  给定长度为n的数组a,定义一次操作为: 1. 算出长度为n的数组s,使得si= (a[1] + a[2] + ... + a[i]) mod 1,000,000,007: 2. ...

  9. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

随机推荐

  1. spring注解配置启动过程

    最近看起spring源码,突然想知道没有web.xml的配置,spring是怎么通过一个继承于AbstractAnnotationConfigDispatcherServletInitializer的 ...

  2. [moka同学笔记]一、Yii2.0课程笔记(魏曦老师教程)

    第一节   第二节             课程内容     

  3. 解决SlidingMenu和SwipeBackLayout右滑事件冲突问题

    SwipeBackLayout向右滑动关闭当前Activity,SlidingMenu向右滑动则是打开menu部分.在同一个Activity中,当SlidingMenu处于打开状态时,此时向右滑动,事 ...

  4. redis 慢日志 slowlog

    1 slowlog是什么 redis的slowlog是redis用于记录记录慢查询执行时间的日志系统.由于slowlog只保存在内存中,因此slowlog的效率很高,完全不用担心会影响到redis的性 ...

  5. weblogic 12c下jxls导出excel报错Could not initialize class org.apache.poi.xssf.usermodel.XSSFVMLDrawing

    周一,开发反馈weblogic 12c下jxls导出excel报错,公司环境和UAT环境均报错,看日志如下: 2016-06-08 09:16:55,825 ERROR org.jxls.util.T ...

  6. 捋一捋Javascript数据类型转换规则

    一.数据类型 5种基本数据类型:Null/Undefined/String/Boolean/Number 1种复杂数据类型:Object 二.数据类型检测 传送门<几种JS数据类型方式及其局限性 ...

  7. xampp 访问出现New XAMPP security concept 解决办法

    最近通过手机访问本地服务器时出现以下问题: Access forbidden! New XAMPP security concept: Access to the requested director ...

  8. sap透明表、结构、簇介绍以及查找表方法

    sap透明表.结构.簇介绍以及查找表方法 一些人在写开发功能说明书的时候不知道如何去找屏幕字段对应的透明表,下面我来介绍一个比较有效的方法:首先简单介绍一下概念:在SAP中的表的种类有以下三种:Tra ...

  9. Android项目实战(十五):自定义不可滑动的ListView和GridView

    不可滑动的ListView (RecyclweView类似) public class NoScrollListView extends ListView { public NoScrollListV ...

  10. UWP开发中的方向传感器

    在UWP开发中,我们能使用的到方向有三种: OrientationSensor下的四元数:Compass罗盘的HeadingMagneticNorth:以及SimpleOrientationSenso ...