2016 ACM/ICPC Asia Regional Shenyang Online HDU 5895 Mathematician QSC
2016-09-20 13:19
633 查看
Mathematician QSC
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 274 Accepted Submission(s): 141
[align=left]Problem Description[/align]
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?
[align=left]Input[/align]
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
[align=left]Output[/align]
For each test case the output is only one integer number ans in a line.
[align=left]Sample Input[/align]
2
20160830 2016 12345678 666
20101010 2014 03030303 333
[align=left]Sample Output[/align]
1
317
【题意】
已知f(0)=0,f(1)=1,f(n)=f(n−2)+2∗f(n−1)(n≥2)
给你n,y,x,s的值
求
的值
【解题方法】可以看这篇blog,写得太好,我不好意思写了。http://blog.csdn.net/queuelovestack/article/details/52577212
【AC 代码】
//
//Created by just_sort 2016/9/20 11:13
//Copyright (c) 2016 just_sort.All Rights Reserved
//
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
LL mod;
struct Matrix{
int row,col;
LL mat[2][2];
void init(int row,int col,bool ok=false){
this->row=row,this->col=col;
memset(mat,0,sizeof(mat));
if(!ok) return ;
for(int i=0; i<row; i++) mat[i][i]=1;
}
Matrix operator*(const Matrix &rhs){
Matrix ret;ret.init(row,rhs.col);
for(int k=0; k<col; k++){
for(int i=0; i<row; i++){
if(mat[i][k]==0) continue;
for(int j=0; j<rhs.col; j++){
if(rhs.mat[k][j]==0) continue;
ret.mat[i][j]+=mat[i][k]*rhs.mat[k][j]%mod;
ret.mat[i][j]%=mod;
}
}
}
return ret;
}
Matrix operator^(LL n){
Matrix ret,x=*this;
ret.init(row,col,1);
while(n){
if(n&1) ret=ret*x;
x=x*x;
n>>=1;
}
return ret;
}
}A,B;
int eluer(int n)
{
int ans=1,i;
for(i=2; i*i<=n; i++)
{
if(n%i==0)
{
n/=i;
ans*=(i-1);
while(n%i==0)
{
n/=i;
ans*=i;
}
}
}
if(n>1) ans*=(n-1);
return ans;
}
LL quickmod(LL a,LL n,LL m)
{
LL res=1,temp=a;
while(n)
{
if(n&1) res = res*temp%m;
temp = (temp*temp)%m;
n>>=1;
}
return res%m;
}
int main()
{
int T,n,y,x,s;
LL z;
scanf("%d",&T);
while(T--)
{
A.init(2,2);
A.mat[0][0]=0;A.mat[0][1]=1;
A.mat[1][0]=1;A.mat[1][1]=2;
B.init(2,2);
B.mat[0][0]=0;B.mat[0][1]=0;
B.mat[1][0]=1;B.mat[1][1]=0;
scanf("%d%d%d%d",&n,&y,&x,&s);
mod = 1LL*2*eluer(s+1);
z=n;
z*=y;
Matrix ans = A^z;
ans = ans*B;
z = (ans.mat[0][0]%mod*ans.mat[1][0]%mod)%mod+mod;
z = (z%mod + mod)/2;
printf("%I64d\n",quickmod(1LL*x,1LL*z,s+1));
}
return 0;
}
相关文章推荐
- 2016 ACM/ICPC Asia Regional Shenyang Online HDU 5895 Mathematician QSC(矩阵快速幂+高次幂取模)★ ★
- HDU 5895 Mathematician QSC(逆元应用+矩阵快速幂+数论知识)——2016 ACM/ICPC Asia Regional Shenyang Online
- HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)——2016 ACM/ICPC Asia Regional Shenyang Online
- HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)——2016 ACM/ICPC Asia Regional Shenyang Online
- HDU 5895&&2016 ACM/ICPC Asia Regional Shenyang Online1004 Mathematician QSC [矩阵加速+欧拉降幂]【数论】
- hdu 5900 QSC and Master 区间dp(2016 ACM/ICPC Asia Regional Shenyang Online)
- HDU 5898 odd-even number (数位DP) 2016 ACM/ICPC Asia Regional Shenyang Online
- 2016 ACM/ICPC Asia Regional Shenyang Online HDU 5898 odd-even number
- HDU 5901 Count Primes (模板 + 数论知识)——2016 ACM/ICPC Asia Regional Shenyang Online
- HDU 5898&&2016 ACM/ICPC Asia Regional Shenyang Online/ odd-even number [数位DP]【动态规划】
- 2016 ACM/ICPC Asia Regional Shenyang Online HDU 5900 QSC and Master(区间dp水题)
- HDU 5900 QSC and Master(2016 ACM/ICPC Asia Regional Shenyang Online)
- HDU 5898 odd-even number (数位DP) 2016 ACM/ICPC Asia Regional Shenyang Online
- 2016 ACM/ICPC Asia Regional Shenyang Online 1003/HDU 5894 数学/组合数/逆元
- 2016 ACM/ICPC Asia Regional Shenyang Online HDU 5893 List wants to travel
- HDU 5898 odd-even number(2016 ACM/ICPC Asia Regional Shenyang Online)
- 2016 ACM/ICPC Asia Regional Shenyang Online HDU 5894 hannnnah_j’s Biological Test
- 2016 ACM/ICPC Asia Regional Shenyang Online 1007/HDU 5898 数位dp
- HDU 5894 hannnnah_j’s Biologica(lucas定理求组合数+乘法逆元)——2016 ACM/ICPC Asia Regional Shenyang Online
- 2016 ACM/ICPC Asia Regional Shenyang Online HDU 5901 Count Primes (模板 + 数论知识)★