hdu 3483 矩阵乘法
2024-10-08 19:00:35
这个题目上周对抗赛题目,搞了我好久 对数学这种不是很敏感
其实都不是自己想出来的,看其他的资料和博客的推导 还是有点难度的,反正我是推不出来
通过二项式定理的化简
有两个博客写得比较好
http://972169909-qq-com.iteye.com/blog/1863402
http://www.cppblog.com/Yuan/archive/2010/08/13/123268.html
反正构造好二项式之后,乘N次,就可以得到结果了,因为右边的式子 初始全部是x。
#include <iostream>
#include <cstring>
#include <cstdio>
#define ll __int64
using namespace std;
const int maxn = ;
ll c[maxn][maxn], a[maxn][maxn], b[maxn][maxn], t[maxn][maxn];
ll N,x,M;
void init()
{
int i, j;
memset(c, , sizeof(c));
for (i = ; i <= x; i++)
{
c[i][] = c[i][i] = ;
for (j = ; j < i; j++)
{
c[i][j] = (c[i-][j] + c[i-][j-]) % M;
}
}
memset(a, , sizeof(a));
for (i = ; i <= x; i++)
{
for (j = ; j <= i; j++)
{
a[i][j] = (c[i][j] * x) % M;
}
}
memcpy(a[x+], a[x], sizeof(a[x]));
a[x+][x+] = ;
memset(b, , sizeof(b));
for (i = ; i <= x+; i++)
{
b[i][i] = ;
}
}
void mul(long long p[maxn][maxn], long long q[maxn][maxn])
{
int i, j, k;
memset(t, , sizeof(t));
for (i = ; i <= x+; i++)
{
for (j = ; j <= x+; j++)
{
for (k = ; k <= x+; k++)
{
t[i][j] = (t[i][j] + p[i][k] * q[k][j]) % M;
}
}
}
memcpy(q, t, sizeof(t));
}
void cal()
{
while (N)
{
if (N & )
{
mul(a, b);
}
mul(a, a);
N >>= ;
}
}
int main()
{
while (scanf("%I64d %I64d %I64d", &N, &x,&M)&& N> && x> &&M>)
{
init();
cal();
printf("%I64d\n", b[x+][]);
}
return ;
}
最新文章
- classpath: VS classpath*:
- 【BZOJ3036】绿豆蛙的归宿 拓补排序+概率
- mvc图片上传到服务器
- 利用LruCache为GridView异步加载大量网络图片完整示例
- Python for Informatics 第11章 正则表达式一(译)
- 简单说一下printf(";%*s%s";,xx,xx,xx);或printf(";%*s\n";,xx,xx);
- jsoup 简介
- 通过案例对 spark streaming 透彻理解三板斧之一: spark streaming 另类实验
- blueImp/jQuery file upload 的正确用法(限制上传大小和文件类型)
- SQLLite 可以通过SQL语言来访问的文件型SQL数据库
- pip是用国内镜像源
- 文字处理控件TX Text Control X10独家揭秘(一):数据源自动处理
- Android热修复
- IT新人论成长
- 简约的单页应用引擎:sonnyJS
- 共有19款Java 文件上传组件开源软件
- PHP &#39;ext/gd/gd.c&#39; gdImageCrop整数符号错误漏洞
- windows下Nginx配置与测试
- rabbitmq Clustering Guide--官方
- [C++][OpenGL]自己写GUI(0)——介绍