题目链接: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. [C/C++语言标准] ISO C99/ ISO C11/ ISO C++11/ ISO C++14 Downloads

    语言法典,C/C++社区人手一份,技术讨(hu)论(peng)必备 ISO IEC C99 https://files.cnblogs.com/files/racaljk/ISO_C99.pdf IS ...

  2. CET——4 常用短语

    在网上看到的,先拔到自己这来,四级大大,求过!!!!

  3. ArcGIS API for JavaScript 4.2学习笔记[24] 【IdentifyTask类】的使用(结合IdentifyParameters类)(第七章完结)

    好吧,我都要吐了. 接连三个例子都是类似的套路,使用某个查询参数类的实例,结合对应的Task类,对返回值进行取值.显示. 这个例子是Identify识别,使用了TileLayer这种图层,数据来自Se ...

  4. 网络实时流量监控工具iftop---转

    网络实时流量监控工具iftop 分类: LINUX 1.安装依赖软件库 [root@localhost ~]# yum install libpcap libpcap-devel ncurses nc ...

  5. php-迭代创建级联目录

    方法一代码: path = './a/b/c/d/e/f'; $path_arr = explode('/',$path);//得到数组array('.','a','b','c','d','e','f ...

  6. sort 命令详解

    sort  作用:将文本文件内容加以排序,sort可针对文本文件的内容,以行为单位来排序  参数: -b 忽略每行前面开始出的空格字符. -c 检查文件是否已经按照顺序排序. -d 排序时,处理英文字 ...

  7. Oracle ADG搭建

    Oracle Active Data Guard搭建 一:安装 1.基础环境配置 1.1.开启强制日志记录 DG日志发送方式中ARCH进程和LGWR进程的ASYNC模式都是基于日志同步的,所以我们必须 ...

  8. websocket教程(一) 非常有趣的理解websocket

    一.websocket与http WebSocket是HTML5出的东西(协议),也就是说HTTP协议没有变化,或者说没关系,但HTTP是不支持持久连接的(长连接,循环连接的不算) 首先HTTP有 1 ...

  9. package-cleanup

    package-cleanup 是一个python开发的命令程序,用来清除本机已安装的.重复的 或孤立的软件包. desktop版的CentOS镜像包含这个工具,而Minimal版的CentOS镜像不 ...

  10. windows上安装redis

    The Redis project does not officially support Windows. However, the Microsoft Open Tech group develo ...