斐波那契数列模$10^m$的循环节为$6\times10^m$,于是从低位到高位dfs即可。

#include<cstdio>
#include<cstring>
#define N 20
typedef long long ll;
typedef unsigned long long ull;
int n,i,flag;char a[N];ll mo[N],b[N];ull ans;
inline ull mul(ull a,ull b,ull P){ull t=0;for(;b;b>>=1,a=(a+a)%P)if(b&1)t=(t+a)%P;return t;}
void cal(ll n,ll&x,ll&y,ll P){
if(!n){x=0,y=1;return;}
if(n==1){x=y=1;return;}
if(n&1){
cal(n-1,y,x,P);
y=(1ULL*y+x)%P;
return;
}
ll a,b;
cal(n>>1,a,b,P);
x=(mul(a,b,P)+mul(a,b<a?b-a+P:b-a,P))%P;
y=(mul(a,a,P)+mul(b,b,P))%P;
}
inline ll fib(ll n,ll P){ll x,y;cal(n,x,y,P);return x;}
void dfs(int n,ll t,ll T){
if(flag)return;
if(fib(t,mo[n])!=b[n])return;
if(n==1){flag=1,ans=6000000000000000000ULL+t;return;}
for(int i=0;i<10;i++)dfs(n-1,(1ULL*t+T*i)%(T*10),T*10);
}
int main(){
scanf("%s",a+1),n=strlen(a+1);
for(mo[i=n]=1;i;i--,mo[i]=mo[i+1]*10)b[i]=mo[i]*(a[i]-'0')+b[i+1];
for(i=1;i<=n;i++)mo[i]*=10;
for(i=0;i<60;i++)dfs(n,i,60);
if(flag)printf("%llu",ans);else puts("NIE");
return 0;
}

  

BZOJ4294 : [PA2015]Fibonacci的更多相关文章

  1. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  2. 算法与数据结构(九) 查找表的顺序查找、折半查找、插值查找以及Fibonacci查找

    今天这篇博客就聊聊几种常见的查找算法,当然本篇博客只是涉及了部分查找算法,接下来的几篇博客中都将会介绍关于查找的相关内容.本篇博客主要介绍查找表的顺序查找.折半查找.插值查找以及Fibonacci查找 ...

  3. #26 fibonacci seqs

    Difficulty: Easy Topic: Fibonacci seqs Write a function which returns the first X fibonacci numbers. ...

  4. 关于java的递归写法,经典的Fibonacci数的问题

    经典的Fibonacci数的问题 主要想展示一下迭代与递归,以及尾递归的三种写法,以及他们各自的时间性能. public class Fibonacci { /*迭代*/ public static ...

  5. 斐波拉契数列(Fibonacci) 的python实现方式

    第一种:利用for循环 利用for循环时,不涉及到函数,但是这种方法对我种小小白来说比较好理解,一涉及到函数就比较抽象了... >>> fibs = [0,1] >>&g ...

  6. fibonacci数列(五种)

    自己没动脑子,大部分内容转自:http://www.jb51.net/article/37286.htm 斐波拉契数列,看起来好像谁都会写,不过它写的方式却有好多种,不管用不用的上,先留下来再说. 1 ...

  7. POJ3070 Fibonacci[矩阵乘法]

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13677   Accepted: 9697 Descri ...

  8. Fibonacci 数列算法分析

    /************************************************* * Fibonacci 数列算法分析 ****************************** ...

  9. 算法系列:Fibonacci

    Copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...

随机推荐

  1. [OpenJudge 3061]Flip The Card

    [OpenJudge 3061]Flip The Card 试题描述 There are N× Ncards, which form an N× Nmatrix. The cards can be p ...

  2. Linux 实现rsyslog日志里面的IP地址记录 未测试

    之前我是在bashrc中添加了一句,让系统操作日志时向rsyslog发送一份内容,现在只要在发送的时候,自己再获取下当前的远程登录IP加进去就可以,像这样 /etc/bashrc sshClientI ...

  3. grep与egrep

    当只有一个匹配条件时:egrep pattern file等价于grep -E pattern file 例如: 当多个匹配条件时,只能用egrep -e pattern1 -e pattern2 - ...

  4. set_include_path详细解释

    zendframework的示例index.php里有这样一句 set_include_path('.' . PATH_SEPARATOR . '../library/'. PATH_SEPARATO ...

  5. sharepoint部件webparth关闭找回的方法

  6. Java for LeetCode 172 Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  7. python 的类变量和对象变量

    python是一种解释性的语言,任何变量可以在使用的时候才声明以及定义,也可以在程序运行的任何位置进行声明和定义新的变量. class Man(object): #直接定义的类的变量,属于类 #其中 ...

  8. iOS PickerView动态加载数据

    将新的数据放入临时数组 NSMutableArray *tmp=[[NSMutableArray alloc] init]; [tmp addObject:[[NSString alloc] init ...

  9. js判断是否为正整数的正则写法 JavaScript正整数正则

    判断是否为正整数 JavaScript正则判断一串数字是否为正整数, 首先要明白这几个问题 1:javascript里会把一串数字前边的0自动屏蔽,(我不知道屏蔽这个词用的是否正确) console. ...

  10. Instruments_Activity Monitor使用入门

    Activity Monitor,官方解释为:(活动监视器)即实时显示CPU.内存和网络的使用情况,记录由虚拟内存大小测量的系统负载.用一句大白话来说,Activity Monitor类似Window ...