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 ...
随机推荐
- ubuntu 12.04 下 eclipse关联 source code
一.JDK source code 命令行中: sudo apt-get install openjdk-7-source 下好的jdk源码在 Linux 在目录 usr/lib/jvm/openjd ...
- [patl2-011]玩转二叉树
解题关键:数据结构课本上的裸题. #include<cstdio> #include<cstdlib> #include<cstring> #include< ...
- Ros学习——创建ROS消息和ROS服务
1.rosed rosed 是 rosbash 的一部分.利用它可以直接通过package名来获取到待编辑的文件而无需指定该文件的存储路径了. rosed默认的编辑器是vim.修改其他的,打开~/.b ...
- 大O表示法总结
大O符号用于计算机科学来描述算法的性能或复杂性.Big O特别描述了最坏的情况,可以用算法来描述所需的执行时间或使用的空间(例如在内存或磁盘上). 任何读过Programming Pearls(编程珠 ...
- IO流对文件的读取操作
/*1. 在当前项目的根目录下有一个名为“info.txt”的文件,里面存放的内容如下(可手动创建录入,不需要使用IO流): 2. 利用IO流的知识读取info.txt文件的内容, 在控制台上打印大写 ...
- 主键primary key和唯一索引unique index
1)主键一定是唯一性索引,唯一性索引并不一定就是主键. 2)主键就是能够唯一标识表中某一行的属性或属性组,一个表只能有一个主键,但可以有多个候选索引. 3)主键常常与外键构成参照完整性约束,防止出现数 ...
- python2 and python3 difference - division
1. python2 2. python3 3.from python environment import py3 features
- 21、conda下载,安装,卸载
参考:https://www.cnblogs.com/Datapotumas/p/6293309.html 1.下载 conda下载网址:https://conda.io/miniconda.html ...
- Luogu 3530 [POI2012]FES-Festival
我是真的不会写差分约束啊呜呜呜…… BZOJ 2788被权限了. 首先对于第一个限制$x + 1 = y$,可以转化成$x + 1 \leq y \leq x + 1$, 所以连一条$(y, x, - ...
- windows7向github提交代码
首先要有一个github账号. 我自己申请了github账号,然后创建一个代码仓库. 这个是我创建好的代码仓库:里面是空的没有代码,我今天从我本地写好的代码,传到github上面去. 首先需要告诉gi ...