A Short problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1716    Accepted Submission(s): 631

Problem Description
  According to a research, VIM users tend to have shorter fingers, compared with Emacs users.
  Hence they prefer problems short, too. Here is a short one:
  Given n (1 <= n <= 1018), You should solve for
g(g(g(n))) mod 109 + 7
  where
g(n) = 3g(n - 1) + g(n - 2)
g(1) = 1
g(0) = 0
 
Input
  There are several test cases. For each test case there is an integer n in a single line.
  Please process until EOF (End Of File).
 
Output
  For each test case, please print a single line with a integer, the corresponding answer to this case.
 
Sample Input
0
1
2
 
Sample Output
0
1
42837
 
Source
 
此题出得比较精妙,
分析:假设g(g(g(n)))=g(x),x可能超出范围,但是由于mod 10^9+7,所以可以求出x的循环节

求出x的循环节后,假设g(g(g(n)))=g(x)=g(g(y)),即x=g(y),y也可能非常大,但是由x的循环节可以求出y的循环节

如何求循环节点:

 /*采用事先处理自己可以求出来*/
LL work(LL mod){
LL a=,b=;
for(LL i=;;++i)
{
a=(b*+a)%mod;
a=a^b;
b=a^b;
a=a^b;
if(a == && b == ) return i;
}

所以依次将mod1带入得到mod2=222222224;

然后将mod2带入得到mod3=183120;

然后就是快速矩阵了。

代码:

 //#define LOCAL
#include<iostream>
#include<cstdio>
#include<cstring>
#define LL __int64
using namespace std;
const int mod1 =;
const int mod2=;
const int mod3=; LL mat[][];
LL ans[][];
LL n; void Matrix(LL a[][],LL b[][],LL mod)
{
LL cc[][]={};
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
for(int k=;k<;k++)
{
cc[i][j]=(cc[i][j]+a[i][k]*b[k][j])%mod;
}
}
}
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
a[i][j]=cc[i][j];
}
}
} void pow(LL w,LL mod)
{
while(w>)
{
if(w&) Matrix(ans,mat,mod);
w>>=;
if(w==)break;
Matrix(mat,mat,mod);
}
}
void input(LL w,LL mod)
{
mat[][]=;
mat[][]=mat[][]=;
mat[][]=;
ans[][]=ans[][]=;
ans[][]=ans[][]=;
pow(w,mod);
// printf("%I64d\n",ans[0][0]);
}
void work(int i,__int64 w)
{
if(i==||w==||w==)
{
if(w==)
printf("0\n");
else if(w==)
printf("1\n");
else
printf("%I64d\n",ans[][]);
return ;
}
LL mod;
if(i==)mod=mod3;
else if(i==)mod=mod2;
else if(i==)mod=mod1;
input(w-,mod);
work(i+,ans[][]);
}
int main()
{
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif
while(scanf("%I64d",&n)!=EOF)
work(,n);
return ;
}

HDU----(4291)A Short problem(快速矩阵幂)的更多相关文章

  1. HDU 4291 A Short problem(矩阵+循环节)

    A Short problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  2. hdu 4291 A Short problem(矩阵+取模循环节)

    A Short problem                                                          Time Limit: 2000/1000 MS (J ...

  3. HDU 4291 A Short problem(2012 ACM/ICPC Asia Regional Chengdu Online)

    HDU 4291 A Short problem(2012 ACM/ICPC Asia Regional Chengdu Online) 题目链接http://acm.hdu.edu.cn/showp ...

  4. 循环节 + 矩阵快速幂 - HDU 4291 A Short problem

    A Short problem Problem's Link Mean: 给定一个n,求:g(g(g(n))) % 1000000007 其中:g(n) = 3g(n - 1) + g(n - 2), ...

  5. HDU 4291 A Short problem 短问题 (递推,找规律)

    题意: 给出递推式 g(n) = 3g(n - 1) + g(n - 2),且g(1) = 1,g(0) = 0.求g( g( g(n))) mod 109 + 7. 思路: 要求的g( g( g(n ...

  6. hdu 4291 A Short problem

    数学题,找循环节!! 首先g(g(g(n)))=g(x) mod 1e9+7 则可知x有循环节1e9+7; 之后x=g(g(n)),则可算出g(n)的循环节,在算出n的循环节就可以了!! 代码如下: ...

  7. HDU - 6395 Sequence (分块+快速矩阵幂)

    给定递推式: 求Fn. 分析:给出的公式可以用快速矩阵幂运算得到,但 P/n 整除对于不同的i,值是不同的. 可以根据P将3-n分成若干块,每块中P整除n的值是相同的.分块的时候要注意判断. 将每块的 ...

  8. CodeForces621E 快速矩阵幂优化dp

    有时些候在用快速矩阵幂优化dp的时候,它的矩阵乘法是不那么容易被具体为题目背景的意思的,大多数时候难以理解矩阵之间相乘的实际意义,正如有时候我们不知道现在在做手头这些事情的意义,但倘若是因一个目标而去 ...

  9. Java大数——快速矩阵幂

    Java大数——快速矩阵幂 今天做了一道水题,尽管是水题,但是也没做出来.最后问了一下ChenJ大佬,才慢慢的改对,生无可恋了.... 题目描述: 给a,b,c三个数字,求a的b次幂对c取余. 数据范 ...

随机推荐

  1. [转]Unity3D协程介绍 以及 使用

    作者ChevyRay ,2013年9月28日,snaker7译  原文地址:http://unitypatterns.com/introduction-to-coroutines/ 在Unity中,协 ...

  2. chmod 无法修改磁盘文件的权限解释 (光盘文件就是只读的,修改不了的)

    我们知道root用户是linux执行权限最高的管理者用户,他可以进行任何的权限操作:然而我们的操作系统同样也考虑过这样的弊端,就是当我们使用者并不了解文件属性和重要性时会给予我们使用者提示: 举个例子 ...

  3. WebRTC的学习(一)

    这篇文章是我翻译mozilla上的英文资料. 英文原文的链接地址为https://developer.mozilla.org/zh-CN/docs/Web/API/WebRTC_API/Protoco ...

  4. CentOS最小化安装后,增加GNOME桌面

    背景:下载CentOS 7的安装包后,在虚拟机上安装. 上来就遇到一个问题:提示需要开启intel vt-x. 这个进入BIOS,在CPU的设置中开启即可. 然后怀着兴奋的心情,开始各种下一步的安装, ...

  5. 【转】Haproxy安装及配置

    1.安装 # wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.20.tar.gz # tar zcvf haproxy-1.3.20.t ...

  6. Expression Blend制作自定义按钮(转)

    来源:http://www.cnblogs.com/iChina/archive/2011/11/25/2262854.html Expression Blend制作自定义按钮 1.从Blend工具箱 ...

  7. Python基础学习笔记(三)运算符

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-chinese-encoding.html 3. http://w ...

  8. 使用Select命令创建菜单

    创建文本菜单的一半功夫都花在了创建菜单布局和获取输入的字符上.bash shell提供了一个很容易上手的小工具来自动完成这些工作select命令允许从单个命令行创建菜单,然后在提取输入的答案并自动处理 ...

  9. iOS - Swift NSFileManage 文件管理

    前言 public class NSFileManager : NSObject public class NSFileHandle : NSObject, NSSecureCoding NSFile ...

  10. nginx+nginx-rtmp-module+ffmpeg搭建流媒体服务器

    参照网址: [1]http://blog.csdn.net/redstarofsleep/article/details/45092147 [2]HLS介绍:http://www.cnblogs.co ...