题目链接:

Mathematician QSC

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

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
 
题意:
 
求上面那个式子的值;
 
思路:
 
难点在怎么推出g[n]的表达式了;g(n)=f(n)*f(n+1)/2;
 
f(n)=f(n-2)+2*f(n-1)
f(n)*f(n-1)=f(n-2)*f(n-1)+2*f(n-1)*f(n-1);
2*f(n-1)*f(n-1)=f(n)*f(n-1)-f(n-2)*f(n-1);
连加得到g(n)=f(n)*f(n+1)/2;
然后就是矩阵快速幂和指数循环节的套路了;
 
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} //const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=(1<<20)+10;
const int maxn=1e5+10;
const double eps=1e-12; LL prime[maxn],mod;
int vis[maxn],cnt=0;
struct matrix
{
LL a[2][2];
};
matrix cal(matrix A,matrix B)
{
matrix 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]+=A.a[i][k]*B.a[k][j];
C.a[i][j]%=mod;
}
}
}
return C;
} LL pow_mod(LL y)
{
if(y==0)return 0;
else if(y==1)return 1;
else if(y==2)return 2;
else y-=2;
matrix s,base;
s.a[0][0]=s.a[1][1]=1;s.a[0][1]=s.a[1][0]=0;
base.a[0][0]=2,base.a[0][1]=base.a[1][0]=1,base.a[1][1]=0;
while(y)
{
if(y&1)s=cal(s,base);
base=cal(base,base);
y>>=1;
}
return (s.a[0][0]*2+s.a[0][1])%mod;
}
inline void Init()
{
for(int i=2;i<maxn;i++)
{
if(!vis[i])
{
for(int j=2*i;j<maxn;j+=i)vis[j]=1;
prime[++cnt]=(LL)i;
}
}
}
LL phi(LL fx)
{
LL s=fx;
for(int i=1;i<=cnt;i++)
{
if(fx<prime[i])break;
if(fx%prime[i]==0)
{
s=s/prime[i]*(prime[i]-1);
while(fx%prime[i]==0)fx/=prime[i];
}
}
if(fx>1)s=s/fx*(fx-1);
return s;
}
LL powmod(LL a,LL b,LL mo)
{
LL s=1,base=a;
while(b)
{
if(b&1)s=s*base%mo;
base=base*base%mo;
b>>=1;
}
return s;
}
int main()
{
Init();
int t;
LL n,y,x,s;
read(t);
while(t--)
{
scanf("%lld%lld%lld%lld",&n,&y,&x,&s);
s++;
mod=phi(s)*2;
LL ans=pow_mod(n*y)*pow_mod(n*y+1)%mod/2+mod/2;
ans=powmod(x,ans,s);
printf("%lld\n",ans);
}
return 0;
}

  

 

hdu-5895 Mathematician QSC(数学)的更多相关文章

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

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

  2. hdu 5895 Mathematician QSC 指数循环节+矩阵快速幂

    Mathematician QSC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  3. HDU 5895 Mathematician QSC

    矩阵快速幂,欧拉定理. $g(n)$递推式:$g(n)=5g(n-1)+5g(n-2)-g(n-3)$,可以构造矩阵快速求递$n$项,指数很大,可以利用欧拉定理降幂. #pragma comment( ...

  4. hdu 5895 广义Fibonacci数列

    Mathematician QSC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  5. hdu 5895(矩阵快速幂+欧拉函数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5895 f(n)=f(n-2)+2*f(n-1) f(n)*f(n-1)=f(n-2)*f(n-1)+2 ...

  6. HDU 4816 Bathysphere(数学)(2013 Asia Regional Changchun)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4816 Problem Description The Bathysphere is a spheric ...

  7. HDU 5584 LCM Walk 数学

    LCM Walk Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5584 ...

  8. HDU 4336 Card Collector 数学期望(容斥原理)

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=4336 题意简单,直接用容斥原理即可 AC代码: #include <iostream> ...

  9. HDU 5570 balls 期望 数学

    balls Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5570 De ...

随机推荐

  1. 常用SQL查询语句

    一.简单查询语句 1. 查看表结构 SQL>DESC emp; 2. 查询所有列 SQL>SELECT * FROM emp; 3. 查询指定列 SQL>SELECT empmo, ...

  2. 安装性能测试工具:sysbench和使用apache的ab

    一.软件的用途,它主要包括以下几种方式的测试:1.cpu性能2.磁盘io性能3.调度程序性能4.内存分配及传输速度5.POSIX线程性能6.数据库性能(OLTP基准测试) 这个软件为什么找不到官网呢? ...

  3. Python生成器、迭代器、可迭代对象

    把一个列表[]改成()就创建了一个生成器:generator,generator保存的是算法. 可以用于for循环的数据类型:1.集合类型:list tuple dict set str2.gener ...

  4. NullPointerException at android.widget.AbsListView.obtainView at android.widget.ListView.makeAndAddView

    使用ExpandableListView的时候,报如下错.网上搜索发现原来是在CommonNumberQueryAdapter的getGroupView()方法里返回的是null,注意细节哦!!! 1 ...

  5. 单例(Singleton pattern)模式的七种写法

    转载请注明出处:http://www.cnblogs.com/smbk/ One: public class Singleton { private static Singleton instance ...

  6. jQuery中的尺寸及位置的取和设

    1.offset(); 获取位置值: $(selector).offset().left; $(selector).offset().top; 设置位置值: $(selector).offset({t ...

  7. Hadoop技术内幕(YARN)第4章问题部分答案

    问题1:改写DistributedShell程序,使得每个container运行在不同节点上(目前是随机的,可能运行在任意节点上). 问题2:改写DistributedShell程序,使得某个用户指定 ...

  8. 基本完成了一个SEGY扫描程序

    利用Seismic.NET编写了一个SEG-Y文件的扫描程序,可以自动判断道头字中主测线号.横测线号.X坐标和Y坐标的位置,自动快速扫描地震数据体中的拐点坐标.10GB多的数据体几十秒全部扫描完成! ...

  9. bash shell命令(1)

    本文地址:http://www.cnblogs.com/archimedes/p/bash-shell1.html,转载请注明源地址. ls命令 ls用来列出目录的内容,它是用户最常用的命令之一,ls ...

  10. 百度Couldn't load BaiduMapSDK_v2_4_1 from loader dalvik

    原文链接:http://liuzhichao.com/p/1463.html 在使用百度定位SDK的时候,明明已经加入了liblocSDK3.so,Manifest中也添加了相应权限,注册了com.b ...