hust1384---The value of F[n]
Description
For any integer i>=3 we have F[i]=(F[i-1]+2*F[i-2]+3*F[i-3])%9901;
Now give you F[0],F[1],F[2],can you tell me the value of F
Input
Fist Line, an integer Q(1<=Q<=100) represent the number of cases;
Every case is a line of four integers:F[0],F[1],F[2],n;
(0<=F[0],F[1],F[2]<9901,0<=n<100000000)
Output
For each case ,you are request to output one integer the value of F[n] in one line.
Sample Input
4
1 2 3 3
4 5 6 5
2 3 4 2
4 5 6 100000
Sample Output
10
129
4
6086
Hint
Source
Dongxu LI
水题,非常easy推出转移矩阵
/*************************************************************************
> File Name: hust1384.cpp
> Author: ALex
> Mail: zchao1995@gmail.com
> Created Time: 2015年03月12日 星期四 13时50分41秒
************************************************************************/
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;
const int mod = 9901;
struct MARTIX
{
int mat[4][4];
MARTIX();
MARTIX operator * (const MARTIX &b)const;
MARTIX& operator = (const MARTIX &b);
};
MARTIX :: MARTIX()
{
memset (mat, 0, sizeof(mat));
}
MARTIX MARTIX :: operator * (const MARTIX &b)const
{
MARTIX res;
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
for (int k = 0; k < 3; ++k)
{
res.mat[i][j] += this -> mat[i][k] * b.mat[k][j];
res.mat[i][j] %= mod;
}
}
}
return res;
}
MARTIX& MARTIX :: operator = (const MARTIX &b)
{
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
this -> mat[i][j] = b.mat[i][j];
}
}
return *this;
}
MARTIX fastpow (MARTIX ret, int n)
{
MARTIX ans;
ans.mat[0][0] = ans.mat[1][1] = ans.mat[2][2] = 1;
while (n)
{
if (n & 1)
{
ans = ans * ret;
}
ret = ret * ret;
n >>= 1;
}
return ans;
}
void Debug(MARTIX A)
{
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
printf("%d ", A.mat[i][j]);
}
printf("\n");
}
}
int F[3];
int main ()
{
int t;
scanf("%d", &t);
while (t--)
{
int n;
scanf("%d%d%d%d", &F[0], &F[1], &F[2], &n);
if (n < 3)
{
printf("%d\n", F[n]);
continue;
}
MARTIX A;
for (int i = 0; i < 3; ++i)
{
A.mat[i][0] = i + 1;
}
A.mat[0][1] = 1;
A.mat[1][2] = 1;
MARTIX F1;
for (int i = 0; i < 3; ++i)
{
F1.mat[0][i] = F[2 - i];
}
MARTIX ans = fastpow(A, n - 2);
ans = F1 * ans;
printf("%d\n", ans.mat[0][0]);
}
return 0;
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
hust1384---The value of F[n]的更多相关文章
- Mysql_以案例为基准之查询
查询数据操作
- 在 C# 里使用 F# 的 option 变量
在使用 C# 与 F# 混合编程的时候(通常是使用 C# 实现 GUI,F#负责数据处理),经常会遇到要判断一个 option 是 None 还是 Some.虽然 Option module 里有 i ...
- 如果你也会C#,那不妨了解下F#(7):面向对象编程之继承、接口和泛型
前言 面向对象三大基本特性:封装.继承.多态.上一篇中介绍了类的定义,下面就了解下F#中继承和多态的使用吧.
- 如果你也会C#,那不妨了解下F#(2):数值运算和流程控制语法
本文链接:http://www.cnblogs.com/hjklin/p/fs-for-cs-dev-2.html 一些废话 一门语言火不火,与语言本身并没太大关系,主要看语言的推广. 推广得好,用的 ...
- 使用F#开发ASP.NET Core应用程序
.NET Core 里的F# 在.NET Core刚发布时,就已经添加了对F#的支持.但因为当时F#组件还不完整,而一些依赖包并没有放在Nuget上,而是社区自己放到MyGet上,所以在使用dotne ...
- 如果你也会C#,那不妨了解下F#(6):面向对象编程之“类”
前言 面向对象的思想已经非常成熟,而使用C#的程序员对面向对象也是非常熟悉,所以我就不对面向对象进行介绍了,在这篇文章中将只会介绍面向对象在F#中的使用. F#是支持面向对象的函数式编程语言,所以你用 ...
- 如果你也会C#,那不妨了解下F#(5):模块、与C#互相调用
F# 项目 在之前的几篇文章介绍的代码都在交互窗口(fsi.exe)里运行,但平常开发的软件程序可能含有大类类型和函数定义,代码不可能都在一个文件里.下面我们来看VS里提供的F#项目模板. F#项目模 ...
- 如果你也会C#,那不妨了解下F#(4):了解函数及常用函数
函数式编程其实就是按照数学上的函数运算思想来实现计算机上的运算.虽然我们不需要深入了解数学函数的知识,但应该清楚函数式编程的基础是来自于数学. 例如数学函数\(f(x) = x^2+x\),并没有指定 ...
- 如果你也会C#,那不妨了解下F#(3):F#集合类型和其他核心类型
本文链接:http://www.cnblogs.com/hjklin/p/fs-for-cs-dev-3.html 在第一篇中,我们介绍了一些基础数据类型,其实那篇标题中不应该含有"F#&q ...
随机推荐
- UVA305 - Joseph(数论 + 打表)
UVA305 - Joseph(数论 + 打表) 题目链接 题目大意:约瑟夫环问题:n个人围成一圈,每次都淘汰第m个人,问最后一个幸存下来的人的编号. 这题的意思有点不一样,它规定前面的k个人是好人, ...
- 大约linux的几个问题,你能回答几个?--回复14-20称号
14.select和poll差异?Poll和epoll的差别? (1)select和poll的差别:(參考:http://blog.csdn.net/mituan2008/article/detail ...
- Could not drop object 'student' because it is referenced by a FOREIGN KEY constraint
1. Find foreign keys SELECT * FROM sys.foreign_keys WHERE referenced_object_id = object_id('Student' ...
- 中颖电子AD操作
#define ADC_DIS 0 #define ADC_ENB 1 //ADC通道号定义 #define ADC_Chanel0 (unsigned char)(0x00<<1) #d ...
- C++四种类型的转换
在C/C++使用的语言 (type) value(您还可以使用type(value))对于显式类型转换,经常提到投.转换程序猿的精度等完全掌握手,一个传统投往往是过度使用.成为C++要根源. 为了降低 ...
- MKNetWorkKit打印URL
-(void)initNewUrl:(NSString *)urlString param:(NSMutableDictionary *)_paramDic{ //拼接參数至URL NSMutable ...
- android Intent.createChooser 应用选择
在微博案例: 1.public void onClickShare(View view) { 2. 3. Intent intent=new Intent(Intent.ACTION_SEND); 4 ...
- Android checkbox和radiobutton 以及Toast和AlertDialog的使用
package com.example.radiobutton_01; import android.app.Activity; import android.os.Bundle; import an ...
- Behavioral模式之Observer模式
1.意图 定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,全部依赖于它的对象都得到通知并被自己主动更新. 2.别名 依赖(dependents).公布-订阅(Publish-Subscr ...
- dom 规划(html和xml)
html dom与xml dom关联: 什么是 DOM? DOM 是 W3C(万维网联盟)的标准. DOM 定义了訪问 HTML 和 XML 文档的标准: "W3C 文档对象模型 (DOM) ...