题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3483

A Very Simple Problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 945    Accepted Submission(s): 471

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
 
Source
 
 //计算排列数(杨辉三角)
//C(m,n) = C(m-1,n-1)+C(m-1,n)
//快速幂
/*
* [题意]
* 输入n, x, m
* 求(1^x)*(x^1)+(2^x)*(x^2)+(3^x)*(x^3)+...+(n^x)*(x^n)
* [解题方法]
* 设f[n] = [x^n, n*(x^n), (n^2)*(x^n),..., (n^x)*(x^n)]
* 则f[n][k] = (n^k)*(x^n)
* 问题转化为求:( g[n] = f[1][x]+f[2][x]+...+f[n][x] )
* 设C(i,j)为组合数,即i种元素取j种的方法数
* 所以有:f[n+1][k] = ((n+1)^k)*(x^(n+1)) (二次多项式展开)
* = x*( C(k,0)*(x^n) +C(k,1)*n*(x^n)+...+C(k,k)*(n^k)*(x^n) )
* = x*( C(k,0)*f[n][0]+C(k,1)*f[n][1]+...+C(k,k)*f[n][k] )
* 所以得:
* |x*1 0................................0| |f[n][0]| |f[n+1][0]|
* |x*1 x*1 0............................0| |f[n][1]| |f[n+1][1]|
* |x*1 x*2 x*1 0........................0| * |f[n][2]| = |f[n+1][2]|
* |......................................| |.......| |.........|
* |x*1 x*C(k,1) x*C(k,2)...x*C(k,x) 0...0| |f[n][k]| |f[n+1][k]|
* |......................................| |.......| |.........|
* |x*1 x*C(x,1) x*C(x,2).......x*C(x,x) 0| |f[n][x]| |f[n+1][x]|
* |0................................0 1 1| |g[n-1] | | g[ n ] |
*/
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define ll long long
const ll maxn = ;
ll c[maxn][maxn];
ll n, mod, x, m;
struct Mat{
ll f[maxn][maxn];
};
void init()
{
ll i,j,k;
c[][] = c[][] = c[][] = ;
for(i = ; i < maxn; i++){
c[i][] = c[i][i] = ;
for(j = ; j < i; j++){
c[i][j] = c[i-][j]+c[i-][j-];
}
}
}
Mat operator *(Mat a, Mat b)
{
ll i, j, k;
Mat c;
memset(c.f,,sizeof(c.f));
for(k = ; k < m; k++){
for(i = ; i < m; i++){
for(j = ; j < m; j++){
if(!b.f[k][j]) continue;
c.f[i][j] = (c.f[i][j]+(a.f[i][k]*b.f[k][j])%mod)%mod;
}
}
}
return c;
}
Mat multi(Mat a,ll b)
{
Mat s;
memset(s.f,,sizeof(s.f));
for(int i = ; i < m; i++){
s.f[i][i] = ;
}
while(b){
if(b&) s = s*a;
a = a*a;
b>>=;
}
return s;
}
int main()
{
init();
while(~scanf("%lld%lld%lld",&n,&x,&mod))
{
if(n<&&x<&&mod<) break;
Mat e;
ll i, j;
ll ans = ;
memset(e.f,,sizeof(e.f));
for(i = ; i <= x; i++){
for(j = i; j <= x; j++){
e.f[j][i] = c[x-i][j-i]*x%mod;
}
}
e.f[][x+] = e.f[x+][x+] = ;
m = x+;
e = multi(e,n);
for(i = ; i < m-; i++) ans = (ans+x*e.f[i][m-])%mod;
printf("%lld\n",(ans+mod)%mod);
}
return ;
}

hdu_3483A Very Simple Problem(C(m,n)+快速幂矩阵)的更多相关文章

  1. hdu_2604Queuing(快速幂矩阵)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2604 Queuing Time Limit: 10000/5000 MS (Java/Others)  ...

  2. Number Sequence(快速幂矩阵)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 Number Sequence Time Limit: 2000/1000 MS (Java/O ...

  3. 矩阵乘法&矩阵快速幂&矩阵快速幂解决线性递推式

    矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b     *     A B   =   a*A+b*C  a*c+b*D c d     ...

  4. 【bzoj4870】[Shoi2017]组合数问题 dp+快速幂/矩阵乘法

    题目描述 输入 第一行有四个整数 n, p, k, r,所有整数含义见问题描述. 1 ≤ n ≤ 10^9, 0 ≤ r < k ≤ 50, 2 ≤ p ≤ 2^30 − 1 输出 一行一个整数 ...

  5. 快速幂 & 矩阵快速幂

    目录 快速幂 实数快速幂 矩阵快速幂 快速幂 实数快速幂 普通求幂的方法为 O(n) .在一些要求比较严格的题目上很有可能会超时.所以下面来介绍一下快速幂. 快速幂的思想其实是将数分解,即a^b可以分 ...

  6. hdu3483 A Very Simple Problem 非线性递推方程2 矩阵快速幂

    题目传送门 题目描述:给出n,x,mod.求s[n]. s[n]=s[n-1]+(x^n)*(n^x)%mod; 思路:这道题是hdu5950的进阶版.大家可以看这篇博客hdu5950题解. 由于n很 ...

  7. 整数快速乘法/快速幂+矩阵快速幂+Strassen算法

    快速幂算法可以说是ACM一类竞赛中必不可少,并且也是非常基础的一类算法,鉴于我一直学的比较零散,所以今天用这个帖子总结一下 快速乘法通常有两类应用:一.整数的运算,计算(a*b) mod c  二.矩 ...

  8. jiulianhuan 快速幂--矩阵快速幂

    题目信息: 1471: Jiulianhuan 时间限制: 1 Sec  内存限制: 128 MB 提交: 95  解决: 22 题目描述 For each data set in the input ...

  9. hiho #1143 : 骨牌覆盖问题·一 (运用快速幂矩阵)

    #1143 : 骨牌覆盖问题·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 骨牌,一种古老的玩具.今天我们要研究的是骨牌的覆盖问题:我们有一个2xN的长条形棋盘,然 ...

随机推荐

  1. http头部 Expect

    本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/90 在通过curl调用对方接口时,发现超时现象很严重,于是询问对 ...

  2. verilog抓外部低频输入信号的上升沿和下降沿

    版权申明:本文为博主窗户(Colin Cai)原创,欢迎转帖.如要转贴,必须注明原文网址 http://www.cnblogs.com/Colin-Cai/p/7220107.html 作者:窗户 Q ...

  3. bat常用命令

    1.@它的作用是隐藏它后面这一行的命令本身(只能影响当前行).2.echo中文为"反馈"."回显"的意思.它其实是一个开关命令,就是说它只有两种状态:打开和关闭 ...

  4. Mysql5.7.20使用group by查询(select *)时出现错误--修改sql mode

    使用select * from 表 group by 字段 时报错 错误信息说明: 1055 - Expression #1 of SELECT list is not in GROUP BY cla ...

  5. IE下判断IE版本的语句...[if lte IE 8]……[endif]

    <!--[if lte IE 6]> <![endif]--> IE6及其以下版本可见   <!--[if lte IE 7]> <![endif]--> ...

  6. 笔记-CGRectInset CGRectoffset UIEdgeInsetsInsetRect 这三个函数的使用情况

    //CGRectInset 将原来的矩形放大或者缩小,正表示缩小,-表示放大. CGRect rect= CGRectMake(20, 50, 100, 80); CGRect rect1=CGRec ...

  7. 阿里云ECS连接阿里云Redis问题

    描述 项目之前的服务器使用Windows,Redis使用阿里云的云数据库Redis版,一切正常. 后来了更换了Linux,也配置好了Redis,但连接阿里云的Redis时却怎么也连接不上 原因 ECS ...

  8. Hibernate学习笔记(4)---hibernate的核心接口

    Configuration类 该类主要是读取配置文件,启动hibernate,并负责管理hibernate的配置信息,一个程序只创建一个Configuration对象. Configuration类操 ...

  9. MicroPython可视化编程开发板—TurnipBit自制MP3教程实例

    转载请以链接形式注明文章来源(MicroPythonQQ技术交流群:157816561,公众号:MicroPython玩家汇) 当前我们都生活在一个有声有色的社会当中,欣赏美丽的景色,享受动人的音乐, ...

  10. TurnipBit-MicroPython开发板:跟孩子一起DIY跳动的心

    天是越来越热了,小心脏也是越跳越快啊,为了表达现在激动的心情,必须做个激动的心开始跳动.紧接着就开始带领大家做个激动的心. 首先说说要借助的平台,这次仅仅需要借助一块TurnipBit开发板. Tur ...