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]的更多相关文章

  1. Mysql_以案例为基准之查询

    查询数据操作

  2. 在 C# 里使用 F# 的 option 变量

    在使用 C# 与 F# 混合编程的时候(通常是使用 C# 实现 GUI,F#负责数据处理),经常会遇到要判断一个 option 是 None 还是 Some.虽然 Option module 里有 i ...

  3. 如果你也会C#,那不妨了解下F#(7):面向对象编程之继承、接口和泛型

    前言 面向对象三大基本特性:封装.继承.多态.上一篇中介绍了类的定义,下面就了解下F#中继承和多态的使用吧.

  4. 如果你也会C#,那不妨了解下F#(2):数值运算和流程控制语法

    本文链接:http://www.cnblogs.com/hjklin/p/fs-for-cs-dev-2.html 一些废话 一门语言火不火,与语言本身并没太大关系,主要看语言的推广. 推广得好,用的 ...

  5. 使用F#开发ASP.NET Core应用程序

    .NET Core 里的F# 在.NET Core刚发布时,就已经添加了对F#的支持.但因为当时F#组件还不完整,而一些依赖包并没有放在Nuget上,而是社区自己放到MyGet上,所以在使用dotne ...

  6. 如果你也会C#,那不妨了解下F#(6):面向对象编程之“类”

    前言 面向对象的思想已经非常成熟,而使用C#的程序员对面向对象也是非常熟悉,所以我就不对面向对象进行介绍了,在这篇文章中将只会介绍面向对象在F#中的使用. F#是支持面向对象的函数式编程语言,所以你用 ...

  7. 如果你也会C#,那不妨了解下F#(5):模块、与C#互相调用

    F# 项目 在之前的几篇文章介绍的代码都在交互窗口(fsi.exe)里运行,但平常开发的软件程序可能含有大类类型和函数定义,代码不可能都在一个文件里.下面我们来看VS里提供的F#项目模板. F#项目模 ...

  8. 如果你也会C#,那不妨了解下F#(4):了解函数及常用函数

    函数式编程其实就是按照数学上的函数运算思想来实现计算机上的运算.虽然我们不需要深入了解数学函数的知识,但应该清楚函数式编程的基础是来自于数学. 例如数学函数\(f(x) = x^2+x\),并没有指定 ...

  9. 如果你也会C#,那不妨了解下F#(3):F#集合类型和其他核心类型

    本文链接:http://www.cnblogs.com/hjklin/p/fs-for-cs-dev-3.html 在第一篇中,我们介绍了一些基础数据类型,其实那篇标题中不应该含有"F#&q ...

随机推荐

  1. Encountered a section with no Package: header

    刚才打开ubuntu,我的版本号是11.04.正想打开新立得软件工具包更新软件的时候,出现了例如以下错误: E:Encountered a section with no Package: heade ...

  2. 排序算法门外汉理解-Shell排序

    #include <stdio.h> /* 希尔排序 基本思想:希尔排序又称为缩小增量排序,对简单插入排序的优化. (外部分组gap,组内部插入排序! ! ) 特点:一种不稳定的排序 */ ...

  3. can&#39;t connect to mysql server on localhost &lt;10061&gt;

    需要启动MySQL服务.它可以通过两种方式来启动使用MySQL: 1.命令行模式. Win+R,进入cmd然后按Enter键.在命令行形式的输入: net start mysql56 mysql56是 ...

  4. 华为-on演习--身高找到最好的二人

    称号: 从5个人选择2作为个人礼仪器.中的每个个体的身高的范围160-190,要求2各高度差值至少(假设差异值同样的事情,他们中最高的选择),输出的两个人的身高升序. Smple input:161  ...

  5. LeetCode Solutions : Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...

  6. android采用videoView播放视频(包装)

    //android播放视频.用法:于androidManifest.xml添加activity, // <activity android:name=".PlayVideo" ...

  7. 什么场景Hbase

    Hbase不太复杂,但适合于存储大量的数据资料.因为是商城系统:用户.商品.订单,店,卖家,这些数据是不适合复杂的关系Hbase. 有一个非常大的数据量订购,并经常来计算.只考虑存款订单Hbase. ...

  8. 它们的定义dialog

    (1)你需要准备自己的自定义对话框样式,是一个布局文件 <?xml version="1.0" encoding="utf-8"? > <Li ...

  9. RH133读书笔记(10)-Lab 10 Exploring Virtualization

    Lab 10 Exploring Virtualization Goal: To explore the Xen virtualization environment and the creation ...

  10. 如何使用autolayout的UIView加入动画

    hi,all: 在经过了一番犹豫之后,我决定将我自己做的这个小APP的源代码发布给大家: 其出发点是和大家一起学习iOS开发,仅供学习參考之用. 之前代码是托管与gitlab 上的.今天我将其pull ...