hdu3483 A Very Simple Problem 非线性递推方程2 矩阵快速幂
题目描述:给出n,x,mod。求s[n].
s[n]=s[n-1]+(x^n)*(n^x)%mod;
思路:这道题是hdu5950的进阶版。大家可以看这篇博客hdu5950题解。
由于n很大,所以肯定是矩阵快速幂的题目,但是矩阵快速幂只能解决线性的问题,n^4在这个式子中是非线性的,后一项和前一项没有什么直接关系,这里要做一个转换,把n^4变成一个线性的,也就是和(n-1)^4有关系的东西,而这个办法就是:
n^4=(n-1+1)^4=(n-1)^4+4*(n-1)^3+6*(n-1)^2+4*(n-1)^1+(n-1)^0;
而x^n是一个线性的东西,只需要在构造A矩阵的时候在矩阵中多乘以一个x就可以了。
但这道题卡住我的地方是,里面需要算组合数,我之前算组合数的方法是用逆元来求,但是逆元只能处理mod为质数的情况,而这道题mod不是质数,所以不可以这样做(题目还算良心,样例给出的就是合数,否则估计wa死),而要用
c[i][j]=c[i-1][j-1]+c[i-1][j]%mod, 来递推,也就是这样。
inline void init() {
cc[0][0]=cc[1][0]=cc[1][1]=1;
for(int i=2;i<=50;i++){
cc[i][0]=cc[i][i]=1;
for(int j=1;j<i;j++){
cc[i][j]=cc[i-1][j]+cc[i-1][j-1]%mod;
}
}
}
而这个矩阵是怎样的呢
完整代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string.h>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#define CLR(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
inline int rd(void) {
int x=0;int f=1;
char s=getchar();
while(s<'0'||s>'9') { if(s=='-')f=-1; s=getchar();}
while(s>='0'&&s<='9') { x=x*10+s-'0'; s=getchar();}
x*=f;return x;}
ll n,x,mod;
ll cc[55][55];
inline void init() {
cc[0][0]=cc[1][0]=cc[1][1]=1;
for(int i=2; i<=50; i++) {
cc[i][0]=cc[i][i]=1;
for(int j=1; j<i; j++) {
cc[i][j]=cc[i-1][j]+cc[i-1][j-1]%mod;
}
}
}
ll f[55],a[55][55];
void mul(ll f[55],ll a[55][55],ll n) {
ll c[55];
CLR(c,0);
for(int j=0; j<n; j++) {
for(int k=0; k<n; k++) {
c[j]=(c[j]+f[k]*a[k][j]%mod)%mod;
}
}
memcpy(f,c,sizeof(c));
}
void mulself(ll a[55][55],ll n) {
ll c[55][55];
CLR(c,0);
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
for(int k=0; k<n; k++) {
c[i][j]=(c[i][j]+a[i][k]*a[k][j]%mod)%mod;
}
}
}
memcpy(a,c,sizeof(c));
}
int main() {
while(scanf("%lld%lld%lld",&n,&x,&mod)) {
if(n==-1)break;
if(n==1) {
printf("%lld\n",x);
} else {
init();
CLR(f,0),CLR(a,0);
f[0]=x;
for(int i=1; i<=x+1; i++) {
f[i]=((ll)pow(2,x-i+1))%mod*x%mod*x%mod;
}
a[0][0]=1,a[1][0]=1;
for(int j=1; j<=x+1; j++) {
for(int i=j; i<=x+1; i++) {
a[i][j]=x*cc[x+1-j][i-j]%mod;
}
}
n--;
for(; n; n>>=1) {
if(n&1)mul(f,a,x+2);
mulself(a,x+2);
}
printf("%lld\n",f[0]);
}
}
}
A Very Simple Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1244 Accepted Submission(s): 608
Problem Description
This is a very simple problem. Given three integers N, x, and M, your task is to calculate out the following value:

Input
There are several test cases. For each case, there is a line with three integers N, x, and M, where 1 ≤ N, M ≤ 2*109, and 1 ≤ x ≤ 50.
The input ends up with three negative numbers, which should not be processed as a case.
Output
For each test case, print a line with an integer indicating the result.
Sample Input
100 1 10000 3 4 1000 -1 -1 -1
Sample Output
5050 444
hdu3483 A Very Simple Problem 非线性递推方程2 矩阵快速幂的更多相关文章
- 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】
还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...
- Luogu3824 [NOI2017]泳池 【多项式取模】【递推】【矩阵快速幂】
题目分析: 用数论分块的思想,就会发现其实就是连续一段的长度$i$的高度不能超过$\lfloor \frac{k}{i} \rfloor$,然后我们会发现最长的非$0$一段不会超过$k$,所以我们可以 ...
- HDU4565 So Easy! —— 共轭构造、二阶递推数列、矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-4565 So Easy! Time Limit: 2000/1000 MS (Java/Others) Memory L ...
- ACM学习历程—SNNUOJ 1116 A Simple Problem(递推 && 逆元 && 组合数学 && 快速幂)(2015陕西省大学生程序设计竞赛K题)
Description Assuming a finite – radius “ball” which is on an N dimension is cut with a “knife” of N- ...
- UVA10655 Contemplation! Algebra —— 推公式、矩阵快速幂
题目链接:https://vjudge.net/problem/UVA-10655 题意: a+b.ab的值分别为p.q,求a^n+b^n. 题解: 1.a.b未知,且直接求出a.b也不太实际. 2. ...
- hdu 2604 Queuing(推推推公式+矩阵快速幂)
Description Queues and Priority Queues are data structures which are known to most computer scientis ...
- HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...
- A Simple Math Problem (矩阵快速幂)
Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x >= 10 f(x) = a0 ...
- HDU-6185-Covering(推递推式+矩阵快速幂)
Covering Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
随机推荐
- 【259】ucpole.dat update
2017年2月21日 57871 +0.020896 0.007232 +0.414732 0.009212 +0.418044 0.007533 p 57872 +0.022055 0.007284 ...
- linux驱动开发的经典书籍
转载于:http://www.cnblogs.com/xmphoenix/archive/2012/03/27/2420044.html 参加实习也近一个月了,严重感觉知识不够,真是后悔学校里浪费那么 ...
- Android 自定义Camera 随笔
一.权限 <uses-permission android:name="android.permission.CAMERA" /> <uses-permiss ...
- sql语句中的问号是干什么的???
1.实例代码一: String sql = "SELECT userid,name FROM tuser WHERE userid=? AND password=?" ; pst ...
- Windows平台上通过git下载github的开源代码
常见指令整理: (1)检查ssh密钥是否已经存在.GitBash. 查看是否已经有了ssh密钥:cd ~/.ssh.示例中说明已经存在密钥 (2)生成公钥和私钥 $ ssh-keygen -t rsa ...
- C++下基本类型所占位数和取值范围
原文:http://hi.baidu.com/magicdemon/blog/item/821b2e22d7df494cad34debd.html C++下基本类型所占位数和取值范围: 符号属性 ...
- session跨域共享
www.maxomnis.com的index.php文件内容 <?phpsession_start();setcookie("user", "alex proter ...
- php学习笔记-while循环
while(condition) { func(); //break the loop } while循环的执行顺序是先判断condition是不是true,如果是true,那么就执行while循环体 ...
- C# 中窗口AutoScaleMode属性
C# 窗体中有一个AutoScaleMode 这个属性,我们大家可能用的比较少. 它的作用是:当屏幕分辨率或字体发生改变时,窗体和控件是如何发生变化的.
- PreparedStatement预编译对象实现
模糊查询 插入 同时插入两行数据 执行更新语句 删除操作