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. 基于微信小程序的失物招领系统的Postmortem

    基于微信小程序的失物招领系统的Postmortem 设想和目标 1.我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 对于我们团队要解决的问题和实现的功能在项目开始就 ...

  2. win7开启wifi

    在启用本地共享连接时,出现的错误! 我已经建了一个无线临时网络,来启用共享用来上网的!Internet连接共享访问被启用时,出现了一个错误(null)?而且这错误也会在系统日志里留下记录,都是些莫名其 ...

  3. 20145237 实验二 “Java面向对象程序设计”

    20145237 实验二 “Java面向对象程序设计” 实验内容 • 理解并掌握面向对象三要素:封装.继承.多态 • 初步掌握UML建模 • 熟悉S.O.L.I.D原则 • 使用TDD设计实现复数类 ...

  4. DML数据操作语言之增加,删除,更新

    1.数据的增加 数据的增加要用到insert语句  ,基本格式是: insert into <表名> (列名1,列名2,列名3,......) values (值1,值2,值3,..... ...

  5. surging教学视频资源汇总

    surging是什么 surging 是一个分布式微服务框架,提供高性能RPC远程服务调用,采用Zookeeper.Consul作为surging服务的注册中心,集成了哈希,随机,轮询.压力最小优先作 ...

  6. JAVA_SE基础——55.自定义异常类

    在Java中已经提供了大量的异常类,但是这些异常类有时野很难满足开发者的要求,所以用户可以根据自己的需要来定义自己的异常类.但自定义的异常类必须继承自Exception或其子类. 可以自定义出的问题称 ...

  7. Ubuntu server 16.04 中文版 终端不能显示中文的解决办法探讨

    对于刚安装成功的Ubuntu server 16.04中文版,在终端显示中文的地方总是出现菱形的图标,看来该版本内置终端暂时不支持中文显示, 还是本人不知道具体操作配置,现通过百度查找以下几个解决方案 ...

  8. Python struct模块

    有的时候需要用python处理二进制数据,比如,存取文件,socket操作时.这时候,可以使用python的struct模块来完成.可以用 struct来处理c语言中的结构体. struct模块中最重 ...

  9. hibernate_exercise-many- to-one(1)

    多对一关系 1.创建t_user表.t_group表 2.在eclipse中创建对应的实体类 package com.eneity; public class User { private int i ...

  10. 算法题丨Move Zeroes

    描述 Given an array nums, write a function to move all 0's to the end of it while maintaining the rela ...