Fibonacci Check-up

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 42 Accepted Submission(s): 27
 
Problem Description
Every ALPC has his own alpc-number just like alpc12, alpc55, alpc62 etc.
As more and more fresh man join us. How to number them? And how to avoid their alpc-number conflicted?
Of course, we can number them one by one, but that’s too bored! So ALPCs use another method called Fibonacci Check-up in spite of collision.

First you should multiply all digit of your studying number to get a number n (maybe huge).
Then use Fibonacci Check-up!
Fibonacci sequence is well-known to everyone. People define Fibonacci sequence as follows: F(0) = 0, F(1) = 1. F(n) = F(n-1) + F(n-2), n>=2. It’s easy for us to calculate F(n) mod m.
But in this method we make the problem has more challenge. We calculate the formula , is the combination number. The answer mod m (the total number of alpc team members) is just your alpc-number.

 
Input
First line is the testcase T.
Following T lines, each line is two integers n, m ( 0<= n <= 10^9, 1 <= m <= 30000 )
 
Output
Output the alpc-number.
 
Sample Input
2
1 30000
2 30000
 
Sample Output
1
3
 
 
Source
2009 Multi-University Training Contest 5 - Host by NUDT
 
Recommend
gaojie
 
/*
题意:给出你公式,让你求( 求和C(k,n)F(k) )%m 初步思路:没思路,先打表
得到:
0
1
3
8
21
55
144
377
987
2584
6765
17711
46368
121393
317811
832040
2178309
5702887
14930352
39088169
102334155
能得出来,G(0)=0;
G(1)=1;
G(n)=3*G(n-1)-G(n-2);
然后用矩阵快速幂求出结果 最重要的构造矩阵还没学线性代数的我只能试着推出来:
|G(n) G(n-1)| | 3 1 |
| 0 0 |*| -1 0 | #总结:板没调好,WA了两发 */
#include<bits/stdc++.h>
using namespace std;
int n,mod;
int t;
/********************************矩阵模板**********************************/
class Matrix {
public:
int a[][];
int n; void init(int x) {
memset(a,,sizeof(a));
if(x){
a[][]=;
a[][]=;
a[][]=-;
a[][]=;
}else{
a[][]=;
a[][]=;
a[][]=;
a[][]=;
}
n=;
} Matrix operator +(Matrix b) {
Matrix c;
c.n = n;
for (int i = ; i < n; i++)
for (int j = ; j < n; j++)
c.a[i][j] = (a[i][j] + b.a[i][j]) % mod;
return c;
} Matrix operator +(int x) {
Matrix c = *this;
for (int i = ; i < n; i++)
c.a[i][i] += x;
return c;
} Matrix operator *(Matrix b)
{
Matrix p;
p.n = b.n;
memset(p.a,,sizeof p.a);
for (int i = ; i < n; i++)
for (int j = ; j < n; j++)
for (int k = ; k < n; k++)
p.a[i][j] = (p.a[i][j] + (a[i][k]*b.a[k][j])%mod) % mod;
return p;
} Matrix power_1(int t) {
Matrix ans,p = *this;
ans = p;
while (t) {
if (t & )
ans=ans*p;
p = p*p;
t >>= ;
}
return ans;
} Matrix power_2(Matrix a,Matrix b,int x){
while(x){
if(x&){
b=a*b;
}
a=a*a;
x>>=;
}
return b;
}
};
/********************************矩阵模板**********************************/
Matrix unit,init;
int main(){
// freopen("in.txt","r",stdin);
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&mod);
unit.init();//存放G(n)的矩阵
init.init();//子矩阵
if(n==){
printf("0\n");
continue;
}else if(n==){
printf("%d\n",%mod);
continue;
}
init=init.power_1(n-);
unit=unit*init;
// cout<<mod<<endl;
// for(int i=0;i<2;i++){
// for(int j=0;j<2;j++){
// cout<<init.a[i][j]<<" ";
// }
// cout<<endl;
// } printf("%d\n",(unit.a[][]+mod)%mod);
}
return ;
}
/*
附上打表小程序
*/
#include<bits/stdc++.h>
using namespace std;
long long Sums(long long n,long long k) //函数定义
{
long long sum = ,N=,K=,M=;
if(k > && k<=n)
{
for(long long i = ;i<=n;i++)
{
N = N * i;
} for(long long j = ;j <= k;j++)
{
K = K * j;
} for(long long h = ;h <= n-k;h++)
{
M = M * h;
}
sum=N/(K*M);
return sum;
}
else
return ;
}
int main(){
//freopen("in.txt","r",stdin);
long long f[];
f[]=;
f[]=;
for(long long i=;i<=;i++)
f[i]=f[i-]+f[i-];
for(long long n=;n<=;n++){
long long cur=;
for(long long k=;k<=n;k++){
long long cnk=Sums(n,k);
//cout<<"C("<<k<<","<<n<<")="<<cnk<<endl;
cur+=cnk*f[k];
}
cout<<cur<<endl;
}
return ;
}

Fibonacci Check-up的更多相关文章

  1. 可变长度的Fibonacci数列

    原题目: Write a recursive program that extends the range of the Fibonacci sequence.  The Fibonacci sequ ...

  2. Applying Eigenvalues to the Fibonacci Problem

    http://scottsievert.github.io/blog/2015/01/31/the-mysterious-eigenvalue/ The Fibonacci problem is a ...

  3. hdu 5167 Fibonacci 打表

    Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Proble ...

  4. 【Fibonacci】BestCoder #28B Fibonacci

    Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  5. hdu 5167 Fibonacci(预处理)

    Problem Description Following is the recursive definition of Fibonacci sequence: Fi=⎧⎩⎨01Fi−1+Fi−2i ...

  6. [Algorithm] Fibonacci Sequence - Anatomy of recursion and space complexity analysis

    For Fibonacci Sequence, the space complexity should be the O(logN), which is the height of tree. Che ...

  7. fibonacci数列的性质和实现方法

    fibonacci数列的性质和实现方法 1.gcd(fib(n),fib(m))=fib(gcd(n,m)) 证明:可以通过反证法先证fibonacci数列的任意相邻两项一定互素,然后可证n>m ...

  8. LeetCode 842. Split Array into Fibonacci Sequence

    原题链接在这里:https://leetcode.com/problems/split-array-into-fibonacci-sequence/ 题目: Given a string S of d ...

  9. LeetCode 873. Length of Longest Fibonacci Subsequence

    原题链接在这里:https://leetcode.com/problems/length-of-longest-fibonacci-subsequence/ 题目: A sequence X_1, X ...

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

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

随机推荐

  1. Codeforce E. Fire

    E. Fire time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  2. python和C语言混编的几种方式

    Python这些年风头一直很盛,占据了很多领域的位置,Web.大数据.人工智能.运维均有它的身影,甚至图形界面做的也很顺,乃至full-stack这个词语刚出来的时候,似乎就是为了描述它. Pytho ...

  3. JSP内置对象的实验报告,页面登陆设计

    JSP内置对象的实验报告 一.实验目的: 本实验的目的是让学生掌握怎样在JSP中使用内置对象request.page.response等. 二.实验要求: 编写四个JSP 页面login.jsp.Re ...

  4. 初学node.js有感二

    node.js进阶 一.回顾与继续   对于一种语言的认识都是经历这样的一个过程的,首先从原生的环境(CMD)中开始学习,找到一门语言之间各种引用的本质和相互之间的调用方式,明澈各种依赖关系,在这个基 ...

  5. Navicat for MySQL:快捷键整理

    使用快捷键,提升工作效率! ctrl+q 打开查询窗口 ctrl+/ 注释sql语句 ctrl+shift +/ 解除注释 ctrl+r 运行查询窗口的sql语句 ctrl+shift+r 只运行选中 ...

  6. RAID及热备盘详解

    RAID,为Redundant Arrays of Independent Disks的简称,中文为廉价冗余磁盘阵列. 一.出现的原因(RAID的优点): 它的用途主要是面向服务器,但现在的个人电脑由 ...

  7. AnsiString和String的区别、使用

    16.C/C++语言在CB中的一些特定用法 2)AnsiString是从Delphi中引进来的吗? 答:CB的核心组件VCL是用Object Pascal语言写出的,所以CB的VCL组件的属性有很多都 ...

  8. Java线程池详解

    一.线程池初探 所谓线程池,就是将多个线程放在一个池子里面(所谓池化技术),然后需要线程的时候不是创建一个线程,而是从线程池里面获取一个可用的线程,然后执行我们的任务.线程池的关键在于它为我们管理了多 ...

  9. uvalive 3029 City Game

    https://vjudge.net/problem/UVALive-3029 题意: 给出一个只含有F和R字母的矩阵,求出全部为F的面积最大的矩阵并且输出它的面积乘以3. 思路: 求面积最大的子矩阵 ...

  10. Zabbix(二) : Zabbix Server端配置文件说明

    Zabbix Server端配置文件说明 # This is a configuration file for Zabbix Server process # To get more informat ...