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. css常用属性1

    1  背景相关 背景颜色 background-color     = 颜色名称/rgb值/十六进制值 背景图片 background-image = url('') 背景图片平铺方式 backgro ...

  2. Angular - Templates(模板)

    点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ 在Angular中,模板是一个包含了Angular特定元素和属性的HTML.Angula ...

  3. 个人从源码理解JIT模式下angular编译AppModule的过程

    承接上文.笔者之前将一个angular项目的启动过程分为了两步: 创建平台得到 PlatformRef ,以及执行平台引用提供的方法编译根模块 AppModule .本文就将着眼于创建好的平台,从an ...

  4. 使用jquery的方法和技巧

    1.下载一个jquery.js的文件 2.引入jquery.js文件 <script type="text/javascript" src="__PUBLIC__/ ...

  5. base64码转图片

    1将图片转换为Base64编码,可以让你很方便地在没有上传文件的条件下将图片插入其它的网页.编辑器中. 这对于一些小的图片是极为方便的,因为你不需要再去寻找一个保存图片的地方. 2.假定生成的代码为& ...

  6. 运行Chromium浏览器缺少google api密钥无法登录谷歌账号的解决办法

    管理员身份运行CMD,然后依次输入以下三行内容: setx GOOGLE_API_KEY "AIzaSyAUoSnO_8k-3D4-fOp-CFopA_NQAkoVCLw"setx ...

  7. uva11806

    [题意] n行m列网格放k个石子.有多少种方法?要求第一行,第一列,最后一行,最后一列必须有石子. [题解] 利用容斥原理.可以转到求"第一行.第一列.最后一行.最后一列没有石子" ...

  8. 关于xamarin.forms 中 list 的loadmore

    前言 最近几天在研究上拉加载啊,下拉刷新啊什么的.然而坑爹的事情总是那么多.在xamarin.forms中,list自带的,并没有上拉加载的这个属性(难道当初他们封装方法时,就不会想到数据多了会咋整吗 ...

  9. apache、php隐藏http头部版本信息的实现方法

    1.apache隐藏头部版本信息,编辑httpd.conf文件,找到: ServerTokens OS ServerSignature On 修改为: ServerTokens ProductOnly ...

  10. 想到一个赚钱的APP

    通过APP上发布调查问卷的需求,鼓励人们注册,并给与一定的报酬.需求主要面向一些调查问卷,一类的需求发布