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. Scrum 冲刺 第五日

    目录 要求 项目链接 燃尽图 问题 今日任务 明日计划 成员贡献量 要求 各个成员今日完成的任务(如果完成的任务为开发或测试任务,需给出对应的Github代码签入记录截图:如果完成的任务为调研任务,需 ...

  2. 在arc模式下 CGImage 释放问题

    //大图bigImage //定义myImageRect,截图的区域 if (imagecount >= 3) { CGRect myImageRect; if (i.size.width< ...

  3. 使用ArrayList时代码内部发生了什么(jdk1.7)?

    前言 ArrayList(这里的ArrayList是基于jdk1.7)是在项目中经常使用的集合类,例如我们从数据库中查询出一组数据.这篇文章不去剖析它的继承和实现,只是让我们知道实例化及增删改查时它的 ...

  4. LeetCode & Q122-Best Time to Buy and Sell Stock II-Easy

    Description: Say you have an array for which the ith element is the price of a given stock on day i. ...

  5. 新概念英语(1-47)A cup of coffee

    新概念英语(1-47)A cup of coffee How does Ann like her coffee? A:Do you like coffee, Ann? B:Yes, I do. A:D ...

  6. Couchbase忘记登录密码怎么办

    以下都为root用户操作: 1.先关闭couchbase /opt/couchbase/etc/couchbase_init.d stop 2.切换到下面的路径,删除这个目录下除logs的所有文件,按 ...

  7. CSS中容易混淆的伪元素类型和用法

    :first-of-type 匹配属于其父元素的第一个特定类型的子元素. 1.例子 <head> <meta charset="UTF-8"> <ti ...

  8. RxJava系列3(转换操作符)

    RxJava系列1(简介) RxJava系列2(基本概念及使用介绍) RxJava系列3(转换操作符) RxJava系列4(过滤操作符) RxJava系列5(组合操作符) RxJava系列6(从微观角 ...

  9. java 中String类的常用方法总结,带你玩转String类。

    String类: String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.String类对象创建后不能修改,StringBuffer & St ...

  10. Java-NIO(八):DatagramChannel

    Java NIO中的DatagramChannel是一个能收发UDP包的通道.操作步骤: 1)打开 DatagramChannel 2)接收/发送数据 同样它也支持NIO的非阻塞模式操作,例如: @T ...