BZOJ4294 : [PA2015]Fibonacci
斐波那契数列模$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的更多相关文章
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- 算法与数据结构(九) 查找表的顺序查找、折半查找、插值查找以及Fibonacci查找
今天这篇博客就聊聊几种常见的查找算法,当然本篇博客只是涉及了部分查找算法,接下来的几篇博客中都将会介绍关于查找的相关内容.本篇博客主要介绍查找表的顺序查找.折半查找.插值查找以及Fibonacci查找 ...
- #26 fibonacci seqs
Difficulty: Easy Topic: Fibonacci seqs Write a function which returns the first X fibonacci numbers. ...
- 关于java的递归写法,经典的Fibonacci数的问题
经典的Fibonacci数的问题 主要想展示一下迭代与递归,以及尾递归的三种写法,以及他们各自的时间性能. public class Fibonacci { /*迭代*/ public static ...
- 斐波拉契数列(Fibonacci) 的python实现方式
第一种:利用for循环 利用for循环时,不涉及到函数,但是这种方法对我种小小白来说比较好理解,一涉及到函数就比较抽象了... >>> fibs = [0,1] >>&g ...
- fibonacci数列(五种)
自己没动脑子,大部分内容转自:http://www.jb51.net/article/37286.htm 斐波拉契数列,看起来好像谁都会写,不过它写的方式却有好多种,不管用不用的上,先留下来再说. 1 ...
- POJ3070 Fibonacci[矩阵乘法]
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13677 Accepted: 9697 Descri ...
- Fibonacci 数列算法分析
/************************************************* * Fibonacci 数列算法分析 ****************************** ...
- 算法系列:Fibonacci
Copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...
随机推荐
- 第8章 Iptables与Firewalld防火墙
章节简述: 红帽RHEL7系统已经用firewalld服务替代了iptables服务,新的防火墙管理命令firewall-cmd与图形化工具firewall-config. 本章节基于数十个防火墙需求 ...
- 序列化.to_sym
<%= link_to t('links.list'), showable_paths[@subchannel_item.showable_type.to_sym], target: '_bla ...
- [codeforces 325]B. Stadium and Games
[codeforces 325]B. Stadium and Games 试题描述 Daniel is organizing a football tournament. He has come up ...
- C++中的vector使用范例
原文链接 http://blog.csdn.net/tjh666/article/details/1604119 1.vector 的数据的存入和输出: #include<stdio.h> ...
- poj1328贪心 雷达,陆地,岛屿问题
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 60381 Accepted: 13 ...
- BestCoder18 1002.Math Problem(hdu 5105) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5105 题目意思:给出一个6个实数:a, b, c, d, l, r.通过在[l, r]中取数 x,使得 ...
- Could not create the view: An unexpected exception was thrown. 电脑突然断电,myeclipse非正常关闭,出现错误
电脑突然断电,myeclipse非正常关闭,“Package Explorer”非正常显示,出现错误“Could not create the view: An unexpected exceptio ...
- css样式自适应,支持数字
td加上style="word-break: break-all;word-wrap: break-word;"样式即可
- 完善DriveInfoEx源代码 获取计算机硬盘序列号
概述: 获取计算机硬盘序列号用途很多,在网上找到了一个C++的源代码DriveInfoEx(点这里查看).非常好的一个DLL,.NET项目可以直接引用,而且源代码里有示例. 但这个DLL在Win7非管 ...
- redhad借用CentOs yum 安装
RedHat linux 默认是安装了yum软件的,但是由于激活认证的原因让redhat无法直接进行yum安装一些软件,如果我们需要在redhat下直接yum安装软件,我们只用把yum的源修改成Cen ...