题目传送门

题目描述:给出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 矩阵快速幂的更多相关文章

  1. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

  2. Luogu3824 [NOI2017]泳池 【多项式取模】【递推】【矩阵快速幂】

    题目分析: 用数论分块的思想,就会发现其实就是连续一段的长度$i$的高度不能超过$\lfloor \frac{k}{i} \rfloor$,然后我们会发现最长的非$0$一段不会超过$k$,所以我们可以 ...

  3. HDU4565 So Easy! —— 共轭构造、二阶递推数列、矩阵快速幂

    题目链接:https://vjudge.net/problem/HDU-4565 So Easy! Time Limit: 2000/1000 MS (Java/Others)    Memory L ...

  4. 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- ...

  5. UVA10655 Contemplation! Algebra —— 推公式、矩阵快速幂

    题目链接:https://vjudge.net/problem/UVA-10655 题意: a+b.ab的值分别为p.q,求a^n+b^n. 题解: 1.a.b未知,且直接求出a.b也不太实际. 2. ...

  6. hdu 2604 Queuing(推推推公式+矩阵快速幂)

    Description Queues and Priority Queues are data structures which are known to most computer scientis ...

  7. 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 ...

  8. 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 ...

  9. HDU-6185-Covering(推递推式+矩阵快速幂)

    Covering Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

随机推荐

  1. Linux 查看一个端口的连接数

    netstat -antp|grep -i "80" |wc -l 譬如查看80端口的连接数

  2. elasticsearch2.x优化小结(单节点)

    最近es一直卡顿,甚至宕机,用bigdesk看了,才晓得,es一直用的默认配置(可以看出我有多懒,先前数据量小,es足以应付,现在数据量上去后就不行了). 这里总结三方面: 1.提升jvm内存 vi ...

  3. __clone()方法

    <?php class NbaPlayer{ public $name; } $james = new NbaPlayer(); $james->name = 'James'; echo ...

  4. [转]SQL 模糊查询

      在进行数据库查询时,有完整查询和模糊查询之分. 一般模糊查询语句如下: SELECT 字段 FROM 表 WHERE 某字段 Like 条件 其中关于条件,SQL提供了四种匹配模式: 1,% :表 ...

  5. Cocos2d-x 网络编程

    主要介绍内容:Http协议,Socket协议,webSocket协议, Cocos2d-x中的相关类和方法 1 Http协议 HTTP协议也叫超文本传输协议.是互联网广泛使用的通信协议,常用于B/S架 ...

  6. C++面向对象类的实例题目十

    题目描述: 编写一个程序,其中有一个汽车类vehicle,它具有一个需要传递参数的构造函数,类中的数据成员:车轮个数wheels和车重weight放在保护段中:小车类car是它的私有派生类,其中包含载 ...

  7. 283E&EZOJ #89 Cow Tennis Tournament

    传送门 分析 我们考虑用所有的情况减去不合法的情况 不难想出所有情况为$C_n^3$ 于是我们考虑不合法的情况 我们知道对于一个不合法的三元组$(a,b,c)$一定是修改后$a<b,b>c ...

  8. Luogu 3233 [HNOI2014]世界树

    BZOJ 3572 首先看出虚树,然后考虑如何$dp$. 我们先在处理出的虚树上$dp$一遍,处理出虚树上所有点距离最近的关键点(关键点一定在虚树上嘛). 具体来说,先搜一遍处理出每一个点的父亲到它的 ...

  9. sublime 配置 anaconda 环境

    安装清单: 软件列表: anaconda sublime text sublime插件列表: package control Conda 安装 anaconda https://www.continu ...

  10. Entity Framework Tutorial Basics(10):Entity Lifecycle

    Entity Lifecycle: Before we work on CRUD operation (Create, Read, Update, Delete), it's important to ...