题目链接:

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. struts2 java.lang.StackOverflowError org.apache.struts2.json.JSONWriter

    1. 问题描述: 页面通过异步访问action,    action的方法通过map封装数据,struts的result的type设置为json,后台报错 六月 25, 2016 6:54:33 下午 ...

  2. java猜数字游戏

    import java.util.Scanner; //导入包 class GuessNum { public static void main(String[] args) { int num = ...

  3. 一个小笔记(7):EN_1

    For nearly ten years, the Unified Modeling Language(UML) has been the industry standard for visualiz ...

  4. 打印机问题win7 和xp

    服务器端问题,重启如下服务 net stop "print spooler" net start "print spooler" gpedit.msc 本地计算 ...

  5. HTML JavaScript的DOM操作

    1.DOM的基本概念 DOM是文档对象模型,这种模型为树模型:文档是指标签文档:对象是指文档中每个元素:模型是指抽象化的东西. 2.Window对象操作 一.属性和方法: 属性(值或者子对象): op ...

  6. ASP.NET本质论第一章网站应用程序学习笔记1

    1.统一资源标示符 1) 格式:协议://主机[.端口号][绝对路径[?参数]],在Http://www.kencery.com/hyl/index/login中,http表示协议的名称,www.ke ...

  7. JY游戏之手游《打卡乐猫》

    JY游戏之手游戏<打卡乐猫> JY是一款专门制作js小游戏的js库,它集成了一些对网页类小游戏的功能帮助 ,能帮你更快的完成js开发,它的主体框架包JY.JYG.Sprite.Sprite ...

  8. 《慕客网:IOS动画案例之会跳动的登入界面(上)》学习笔记 -Sketch的使用

    选中需要放进文件夹里的文件,然后按command+G,就会自动生成文件夹并把需要放进的文件包含进去了.(组 Group) 选中需要锁住的图层,然后按command+shift+L就可以将图层锁住.(锁 ...

  9. android5.x新特性之Tinting

    Android5.X对图形操作上有更多的功能.下面来看看Tinting(着色) Tinting的使用非常简单,几乎 没什么好说的,只要在xml中配置好tint和tintMode即可.直接看实际例子吧. ...

  10. 一个超复杂的间接递归——C语言初学者代码中的常见错误与瑕疵(6)

    问题: 问题出处见 C语言初学者代码中的常见错误与瑕疵(5) . 在该文的最后,曾提到完成的代码还有进一步改进的余地.本文完成了这个改进.所以本文讨论的并不是初学者代码中的常见错误与瑕疵,而是对我自己 ...