Mathematician QSC

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 229    Accepted Submission(s): 114

Problem Description
QSC dream of becoming a mathematician, he believes that everything in this world has a mathematical law.

Through unremitting efforts, one day he finally found the QSC sequence, it is a very magical sequence, can be calculated by a series of calculations to predict the results of a course of a semester of a student.

This sequence is such like that, first of all,f(0)=0,f(1)=1,f(n)=f(n−2)+2∗f(n−1)(n≥2)Then the definition of the QSC sequence is g(n)=∑ni=0f(i)2. If we know the birthday of the student is n, the year at the beginning of the semester is y, the course number x and the course total score s, then the forecast mark is xg(n∗y)%(s+1).
QSC sequence published caused a sensation, after a number of students to find out the results of the prediction is very accurate, the shortcoming is the complex calculation. As clever as you are, can you write a program to predict the mark?

 
Input
First line is an integer T(1≤T≤1000).

The next T lines were given n, y, x, s, respectively.

n、x is 8 bits decimal integer, for example, 00001234.

y is 4 bits decimal integer, for example, 1234.
n、x、y are not negetive.

1≤s≤100000000

 
Output
For each test case the output is only one integer number ans in a line.
 
Sample Input
2
20160830 2016 12345678 666
20101010 2014 03030303 333
 
Sample Output
1
317
/*
hdu 5895 广义Fibonacci数列 problem:
求x^g(n*y)%(s+1)的值. f(0)=0,f(1)=1,f(n)=f(n−2)+2∗f(n−1) g(n)是前n项f的平方和 solve:
因为n*y很大,所以可以通过欧拉降幂. 而且找规律发现g(n*y) = f(n*y)*f(n*y+1)/2
所以可以通过矩阵快速幂求出这两个值,然后用求个逆元 hhh-2016-09-19 20:00:04
*/
#pragma comment(linker,"/STACK:124000000,124000000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <vector>
#include <math.h>
#include <queue>
#include <set>
#include <map>
#define lson i<<1
#define rson i<<1|1
#define ll long long
#define clr(a,b) memset(a,b,sizeof(a))
#define scanfi(a) scanf("%d",&a)
#define scanfs(a) scanf("%s",a)
#define scanfl(a) scanf("%I64d",&a)
#define scanfd(a) scanf("%lf",&a)
#define key_val ch[ch[root][1]][0]
#define eps 1e-7
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
const double PI = acos(-1.0);
ll mod ;
struct Matri
{
ll a[2][2];
};
Matri Mat;
Matri from;
Matri Mul(Matri A,Matri B)
{
Matri c;
for(int i=0; i<2; i++)
{
for(int j=0; j<2; j++)
{
c.a[i][j]=0;
for(int k=0; k<2; k++)
{
c.a[i][j]=(c.a[i][j]+((A.a[i][k]%mod)*(B.a[k][j]%mod)))%mod;
}
}
}
return c;
}
ll Euler (ll n)
{
ll i,ans=n;
for (i=2; i*i<=n; i++) if (n%i==0)
{
ans=ans/i*(i-1);
while (n%i==0) n/=i;
}
if (n>1) ans=ans/n*(n-1);
return ans;
}
Matri Pow(ll n)
{
Matri t ;
Matri ta = Mat ;
t.a[0][0] = t.a[1][1] = 1;
t.a[1][0] = t.a[0][1] = 0;
while(n)
{
if(n & 1) t = Mul(t,ta);
ta = Mul(ta,ta);
n >>= 1;
}
return t;
}
Matri Add(Matri a,Matri b)
{
Matri c;
for(int i=0; i<2; i++)
{
for(int j=0; j<2; j++)
{
c.a[i][j]=a.a[i][j]+b.a[i][j];
c.a[i][j]%=mod;
}
}
return c;
}
ll PowerMod(ll a, ll b, ll c)
{ ll ans = 1;
ll k = a % c;
while(b>0)
{
if(b % 2 == 1)
ans = ((ans%c)*(k%c)) % c;
b = b/2;
k = ((k% c )* (k% c) )% c;
}
return ans; }
void solve(char *s,ll &ans)
{
ans = 0;
for(unsigned int i = 0; i < strlen(s); i++)
{
ans = ans*10 + (s[i] - '0');
}
}
char str1[10],str2[10],str3[10];
int main()
{ ll x,y,s,n;
int T;
scanf("%d",&T);
while(T--)
{
memset(from.a,0,sizeof(from.a));
from.a[0][0] = 1;
Mat.a[0][0] = 2,Mat.a[0][1] = 1,Mat.a[1][1] = 0,Mat.a[1][0] = 1;
scanf("%s%s%s%I64d",str1,str2,str3,&s);
solve(str1,n),solve(str2,y),solve(str3,x);
if(n*y == 0)
{
printf("1\n");
continue;
}
mod = Euler(s+1LL);
mod*=2LL;
Matri tp = Mul(Pow(n*y-1LL),from);
Matri tp1 = Mul(Pow(n*y),from);
ll ta = tp.a[0][0]*tp1.a[0][0]%mod;
ta=ta%(mod)/2LL;
ll to = ta+mod;
printf("%I64d\n",PowerMod(x,to,s+1LL));
}
return 0;
}

  

hdu 5895 广义Fibonacci数列的更多相关文章

  1. 特征根法求通项+广义Fibonacci数列找循环节 - HDU 5451 Best Solver

    Best Solver Problem's Link Mean: 给出x和M,求:(5+2√6)^(1+2x)的值.x<2^32,M<=46337. analyse: 这题需要用到高中的数 ...

  2. 广义Fibonacci数列找循环节 学习笔记

    遇到了2019ICPC南昌赛区的网络赛的一道题,fn=3*fn-1+2*fn-2,有多次询问求fn.总结起来其实就是在模P意义下,O(1)回答广义斐波那契额数列的第n项,可以说是一道模板题了. 这道题 ...

  3. 广义Fibonacci数列模n的循环节

    见这里:http://blog.csdn.net/ACdreamers/article/details/25616461 有详细的分析推理 只找出了循环节的上限,设 f[n] = (af[n - 1] ...

  4. [hdu 1568] Fibonacci数列前4位

    2007年到来了.经过2006年一年的修炼,数学神童zouyu终于把0到100000000的Fibonacci数列(f[0]=0,f[1]=1;f[i] = f[i-1]+f[i-2](i>=2 ...

  5. Fibonacci 数列算法分析

    /************************************************* * Fibonacci 数列算法分析 ****************************** ...

  6. 可变长度的Fibonacci数列

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

  7. HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)

    传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/detai ...

  8. 入门训练 Fibonacci数列

      入门训练 Fibonacci数列   时间限制:1.0s   内存限制:256.0MB 问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时, ...

  9. fibonacci 数列及其应用

    fibonacci 数列及其延展 fibonacci计算 fibonacci数列是指 0,1,1,2,3,5,8,13,21……这样自然数序列,即从第3项开始满足f(n)=f(n-1)+f(n-2): ...

随机推荐

  1. 转 Eclipse快捷键调试大全

    (1)Ctrl+M --切换窗口的大小(2)Ctrl+Q --跳到最后一次的编辑处(3)F2      ---重命名类名 工程名 --当鼠标放在一个标记处出现Tooltip时候按F2则把鼠标移开时To ...

  2. Java语言基础组成

    写完才发现,这个博客不提供目录这个功能,真是想骂爹了...... 目录 关键字 标识符 注释 常量和变量 运算符 语句 函数 数组 1.关键字 描述:刚刚开始学这个的时候,真是傻傻分不清楚,不过没关系 ...

  3. Struts2之Struts2的下载与安装

    Struts2的下载 登陆struts的官网 下载Full Distribution这个选项的struts2的包. 这是Struts2的完整版,里面包括Struts2的实例应用,空实例应用,核心库,源 ...

  4. Mysql主从复制架构实战

    [root@Mysql-master ~]# vim /etc/my.cnf log-bin=mysql-bin server-id = 1  #slave端server-id值改成2 mysql&g ...

  5. python入门(7)Python程序的风格

    python入门(7)Python程序的风格 Python采用缩进方式,写出来的代码就像下面的样子: # print absolute value of an integer: a = 100 if ...

  6. cv2.cornerHarris()详解 python+OpenCV 中的 Harris 角点检测

    参考文献----------OpenCV-Python-Toturial-中文版.pdf 参考博客----------http://www.bubuko.com/infodetail-2498014. ...

  7. JavaScript中的 原型 property 构造函数 和实例对象之间的关系

    1 为什么要使用原型? /* * javascript当中 原型 prototype 对象 * * */ //首先引入 prototype的意义,为什么要使用这个对象 //先来写一个构造函数的面向对象 ...

  8. jscript定时器,一直用的东西,你真的明白吗?

    JavaScript定时器 JavaScript是一种解释型语言(边编译边执行),Js解析顺序是从上到下,然后将编译后的任务丢到一个事件队列中,然后事件内的函数会从上到下开始执行 setInterva ...

  9. Python/MySQL(四、MySQL数据库操作)

    Python/MySQL(四.MySQL数据库操作) 一.数据库条件语句: case when id>9 then ture else false 二.三元运算: if(isnull(xx)0, ...

  10. git出现错误原因解释

    原因,在pull下拉代码或者push之前,你本地还有代码没有进行commit. 引起下面的错误.   建议commit后先pull再看看有没有冲突在进行push. git.exe push --pro ...