题目链接:

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. 【GOF23设计模式】适配器模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_适配器模式.对象适配器.类适配器.开发中场景  适配器模式  笔记本电脑只有USB接口,新买的键盘是PS2接口的,需要用适 ...

  2. 为什么要用visibility:hidden;代替display:none;?

    为什么要用用visibility:hidden;代替display:none;?因为后者更加消耗浏览器: css绘制画面有两种形式:repaint 和reflow,当我们更改css属相如backgro ...

  3. 数码管问题(c++实现)

    描述:液晶数码管用七笔阿拉数字表示的十个数字,把横和竖的一 个短划都称为一笔,即7有3笔,8有7笔等.对于十个数字一种排列,要做到 两相邻数字都可以由另一个数字加上几笔或减去几笔组成,但不能又加又减. ...

  4. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q125-Q126)

    Question 125You are creating an application for SharePoint Server 2010.The application will run on a ...

  5. 一些C语言学习的国外资源

    下面的列表是在网上收集整理的C语言资料,包括PDF等多种格式 A Tutorial on Pointers and Arrays in C Beej's Guide to C Programming ...

  6. 认识Runtime2

    我定义了一个Person类作为测试. 其中Person.h: // // Person.h // Test // // Created by zhanggui on 15/8/16. // Copyr ...

  7. IOS 网络浅析 (二 网络异步请求)

    学习网络,无论是C/S还是B/S首要的当然是向服务器发送请求,并得到响应,么有请求没有响应,那就不叫做网络了. 这边文章向大家介绍境界一下网路异步请求. *大家不要觉得我写的知识点太零散,我只是想给大 ...

  8. 【转】self.myOutlet=nil、viewDidUnload、dealloc的本质剖析

    对于iphone开发人员来说,内存管理是极为重要的技巧,哪怕程序的功能再强大,设计再漂亮,如果内存控制不好,也难逃程序莫名退出的噩运,这与网页开发是完全不同的. 内存控制里面有很多门道,在这里分析一下 ...

  9. Xcode6.4注册URL Scheme步骤详解

    URL Scheme的作用 我们都知道苹果手机中的APP都有一个沙盒,APP就是一个信息孤岛,相互是不可以进行通信的.但是iOS的APP可以注册自己的URL Scheme,URL Scheme是为方便 ...

  10. android studio annotation 配置过程

    参考了好些配置,发现总有这样,那样的问题. 环境:androidstudio 1.5 preview 2 sdk 6.0 1.首先新建一个android项目. 过程略 2.配置project的buil ...