【HDU 3483】 A Very Simple Problem (二项式展开+矩阵加速)
A Very Simple Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1022 Accepted Submission(s): 500Problem DescriptionThis is a very simple problem. Given three integers N, x, and M, your task is to calculate out the following value:
InputThere 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.OutputFor each test case, print a line with an integer indicating the result.Sample Input100 1 100003 4 1000-1 -1 -1Sample Output5050444Source
【分析】
感觉我的思想离正解还是太远太远,根本不会这样想。
首先我们发现x很小,k很大,k达到了10^9,for一遍都不行,这样我们就想到ans可能存在递推式,然后可以用矩阵加速。


转自:http://972169909-qq-com.iteye.com/blog/1863402
太厉害了,我自己都好难再推一次~~
代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define Maxn 60
#define LL long long LL c[Maxn][Maxn];
LL n,x,M; struct node
{
LL a[Maxn][Maxn];
}t[]; void init()
{
memset(c,,sizeof(c));
for(LL i=;i<=;i++) c[i][]=;
for(LL i=;i<=;i++)
for(LL j=;j<=;j++)
c[i][j]=(c[i-][j-]+c[i-][j])%M;
} void get_un()
{
memset(t[].a,,sizeof(t[].a));
for(LL i=;i<=x+;i++) t[].a[i][i]=%M;
} void mul(LL now,LL y,LL z)
{
for(int i=;i<=x+;i++)
for(int j=;j<=x+;j++)
{
t[].a[i][j]=;
for(LL k=;k<=x+;k++)
t[].a[i][j]=(t[].a[i][j]+t[y].a[i][k]*t[z].a[k][j])%M;
}
t[now]=t[];
} void qpow(LL b)
{
get_un();
while(b)
{
if(b&) mul(,,);
mul(,,);
b>>=;
}
} int main()
{
while()
{
scanf("%lld%lld%lld",&n,&x,&M);
if(n==-&&x==-&&M==-) break;
init();
for(LL i=;i<=x;i++)
{
for(LL j=;j<=i;j++) t[].a[i][j]=(x*c[i][j])%M;
for(LL j=i+;j<=x+;j++) t[].a[i][j]=;
}
for(LL i=;i<x;i++) t[].a[x+][i]=;
t[].a[x+][x]=t[].a[x+][x+]=%M;
// get_un();
qpow(n+);
printf("%lld\n",t[].a[x+][]);
}
return ;
}
[HDU 3483]
2016-09-25 22:07:05
【HDU 3483】 A Very Simple Problem (二项式展开+矩阵加速)的更多相关文章
- 【 CodeForces - 392C】 Yet Another Number Sequence (二项式展开+矩阵加速)
Yet Another Number Sequence Description Everyone knows what the Fibonacci sequence is. This sequence ...
- hdu 3483 A Very Simple Problem
两种构造的方式都是正确的: 1. #include<cstdio> #include<cstring> #include<algorithm> #define ma ...
- hdu 5349 MZL's simple problem
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5349 MZL's simple problem Description A simple proble ...
- HDU 4267 A Simple Problem with Integers 多个树状数组
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K ...
- 2015 Multi-University Training Contest 5 hdu 5349 MZL's simple problem
MZL's simple problem Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- HDU 3468:A Simple Problem with Integers(线段树+延迟标记)
A Simple Problem with Integers Case Time Limit: 2000MS Description You have N integers, A1, A2, ... ...
- hdu3483之二项式展开+矩阵快速幂
A Very Simple Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- HDU 5564 Clarke and digits 状压dp+矩阵加速
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5564 题意: 求长度在[L,R]范围,并且能整除7的整数的总数. 题解: 考虑最原始的想法: dp[ ...
- hdu_3483A Very Simple Problem(C(m,n)+快速幂矩阵)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3483 A Very Simple Problem Time Limit: 4000/2000 MS ( ...
随机推荐
- 将table内容输出为csv文件
1.创建 directory create or replace directory tt as'c:\'; SELECT * FROM DBA_OBJECTS DO WHERE DO.OBJECT_ ...
- String filePath = request.getSession().getServletContext().getRealPath("/");这句话返回的路径是什么,解释下getRealPath("/")函数中的"/"表示什么意思
request.getSession().getServletContext() 获取的是Servlet容器对象,相当于tomcat容器了.getRealPath("/") 获取实 ...
- Hibernate注解错误之- org.hibernate.MappingException: Could not determine type for:
Hibernate 注解 @OneToOne 时候,出现以下错误,经调试,发现 注解要么全部放在字段上,要么全部放在get方法上,不能混合使用! org.hibernate.MappingExcept ...
- SQL Server游标+延迟执行简介
在项目测试中,我们可能会使用批量生成数据来测试程序的性能. 这里讲一个我遇到的问题,由于我们批量生成数据时基本上是瞬间完成,所以GETDATE()函数获得的时间基本上也是一样的,而我们又要求生成每条数 ...
- 20160418javaweb之 Filter过滤器
Servlet规范中 Servlet Listener Filter 1.开发Filter 想要开发一个过滤器需要如下两个步骤: (1)写一个类实现特定的接口Filter 生命周期:当服务器启动时,w ...
- SQL Server调优系列基础篇 - 并行运算总结(二)
前言 上一篇文章我们介绍了查看查询计划的并行运行方式. 本篇我们接着分析SQL Server的并行运算. 闲言少叙,直接进入本篇的正题. 技术准备 同前几篇一样,基于SQL Server2008R2版 ...
- thinkphp之wampserver安装
1.如何修改www目录 打开httpd.conf(wamp\bin\apache\Apache2.4.4\conf): 把DocumentRoot "c:/wamp/www" 修改 ...
- iOS--检测野指针
定位野指针除了使用Malloc Scribble(内存涂鸦)外,还可以使用僵尸对象.所谓的僵尸对象,就是将被释放的对象标记为僵尸,系统不会回收这些对象的内存,并让这些内存无法被重用,因而也就不会被覆写 ...
- CSS3中的transform
CSS3 转换,我们能够对元素进行移动.缩放.转动.拉长或拉伸. transform 在2D里主要是4个方法.除了rotate 其他都是接受x y值 translate skew rotate sca ...
- ERROR 1045 (28000): Access denied for user 'root'@'localhost'
# /etc/init.d/mysql stop# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &# mysq ...
