hdu-5895 Mathematician QSC(数学)
题目链接:
Mathematician QSC
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 189 Accepted Submission(s): 90
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?
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
#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(数学)的更多相关文章
- HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)
传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/detai ...
- hdu 5895 Mathematician QSC 指数循环节+矩阵快速幂
Mathematician QSC Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- HDU 5895 Mathematician QSC
矩阵快速幂,欧拉定理. $g(n)$递推式:$g(n)=5g(n-1)+5g(n-2)-g(n-3)$,可以构造矩阵快速求递$n$项,指数很大,可以利用欧拉定理降幂. #pragma comment( ...
- hdu 5895 广义Fibonacci数列
Mathematician QSC Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- 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 ...
- HDU 4816 Bathysphere(数学)(2013 Asia Regional Changchun)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4816 Problem Description The Bathysphere is a spheric ...
- HDU 5584 LCM Walk 数学
LCM Walk Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5584 ...
- HDU 4336 Card Collector 数学期望(容斥原理)
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=4336 题意简单,直接用容斥原理即可 AC代码: #include <iostream> ...
- HDU 5570 balls 期望 数学
balls Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5570 De ...
随机推荐
- If you insist running as root, then set the environment variable RUN_AS_USER=root...
版权声明:本文为博主原创文章,不经博主允许注明链接即可转载. If you insist running as root, then set theenvironment variable RUN_A ...
- 部署时,出现用户代码未处理 System.Security.Cryptography.CryptographicException 错误解决方法
转载:http://www.cnblogs.com/jys509/p/4499978.html 在调用RSA加密的.pfx密钥时,在本地调试没有问题,可以布署到服务器,就会报以下的错误: 用户代码未处 ...
- 轻量级SaaS在线作图工具ProcessOn
俗话说“一图胜千言”,在办公应用领域,流程图是一个非常好的表现企业业务流程或工作岗位规范等内容的展现形式,比如去给客户做调研,回来后都要描述出客户的关键业务流程,谁.什么时候.在什么地方.负责什么事情 ...
- jquery实现页面控件拖动效果js代码
;(function($) { var DragPanelId = "divContext"; var _idiffx = 0; var _idiffy = 0; var _Div ...
- Underscore学习笔记1
项目用了很久underscore.每次都是临时查手册,没有系统的研究过,最近有空正好看看 github地址:https://github.com/lily1010/underscore_learn 一 ...
- Mybatis学习记录(三)----理解SqlMapConfig.xml文件
SqlMapConfig.xml mybatis的全局配置文件SqlMapConfig.xml,配置内容如下: properties(属性) settings(全局配置参数) typeAliases( ...
- Android Sqlite 实例入门
通过一个简单的例子来学习Sqlite,学生选课系统,一开始的需求是学生可以选课,选课完成后可以查询到已经选择的课. 首先设计三个表,学生,课程,选课.学生表存储学生的信息,课程表存储课程的信息,选课表 ...
- IOS单例
单例就是只有一个实例. 两种常见的创建方法: 1. : static A *a = nil; + (A *)shareInstance { if (!a) a = [[self alloc] init ...
- 得到设备是何种iPhone设备 + 怎么获得启动页面图片
一.前言 今天做一个功能,需要动态的获得启动页,然后根据不同设备去使用不用的启动页图片. 二.正文 常规来说,我们直接判断是何种设备,然后通过name去获得图片选择性加载即可.但是实际上遇到的两个问题 ...
- MyBatis入门(一)---基本使用
一.MyBatis简介 1.1.概述 MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架. MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集. M ...