题目链接:

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. 发布ASP.NET Core程序到Linux生产环境

    原文翻译:Publish to a Linux Production Environment 作者:Sourabh Shirhatti 在这篇文章里我们将介绍如何在 Ubuntu 14.04 Serv ...

  2. 我所了解的WEB开发(4) - 神奇的URL

    如果说WEB开发中什么最为神奇的话,我觉得这当属URL了.         URL的启蒙应该是当年的互联网冲浪(surf the Internet),记得当年的电视上宣传互联网冲浪就好似今天对大数据的 ...

  3. [Architecture Design] 累进式Domain Layer

    [Architecture Design] 累进式Domain Layer 前言 本篇的内容大幅度的简化了分析设计.面向对象等等相关知识,用以传达累进式Domain Layer的核心概念.实际开发软件 ...

  4. js验证真实姓名与身份证号,手机号

    最近的项目中用的需要调用实名认证的接口,实名认证接口价格相比短信而言高了不是几分钱,所以说调用实名认证的条件就要严格把关,因此用到js验证真实姓名与js验证身份证号. 进入正题 1.js验证真实姓名 ...

  5. mac java 环境设置

    MAC下JDK1.6下载路径 http://support.apple.com/kb/DL1572 Mac OS的java版本问题和Eclipse中无法找到jdk源代码的问题解决办法 下载包含源代码j ...

  6. Android使用layer-list实现三面边框

    layer-list可以将多个图片或形状按照顺序层叠起来 <?xml version="1.0" encoding="utf-8"?> <la ...

  7. 让 Popwindow 向上弹出

    /** * 获取父控件的位置y-popwindow的高度 = 应该显示的y坐标. x这里设置为center 不刻意指定坐标 注意:控件坐标永远是 左上角坐标! * * @param parent */ ...

  8. 【读书笔记】iOS-GCD-block-后台运行

    当一个app按home键退出的时候,只有最多5秒的时间做一些保存或清理资源的工作.但是调用beginBackgroundTaskWithExpirationHandler方法,可以最多有10分时间在后 ...

  9. 【转】IOS高级教程1:处理1000张图片的内存优化

    转载请保留以下原文链接: http://my.oschina.net/taptale/blog/91894 一.项目需求 在实际项目中,用户在上传图片时,有时会一次性上传大量的图片.在上传图片前,我们 ...

  10. 【转】IOS动画的实现,其实很简单

    动画效果提供了状态或页面转换时流畅的用户体验,在iOS系统中,咱们不需要自己编写绘制动画的代码,Core Animation提供了丰富的api来实现你需要的动画效果.UIKit只用UIView来展示动 ...