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): 88 Accepted Submission(s): 55 |
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 |
Sample Output
5050 |
Source
2010 ACM-ICPC Multi-University Training Contest(5)——Host by BJTU
|
Recommend
zhengfeng
|
/*
题意:给你n,x,m,让你求1^x*x^1+2^x*x^2+...+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][k]+F[2][k]+...+F[n][k];
设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<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,x,mod;
ll c[][];
ll unit;
/********************************矩阵模板**********************************/
class Matrix {
public:
ll a[][];
int n;
void init() {
memset(a,,sizeof(a));// #出错
for(int i=;i<=n;i++){
for(int j=;j<=i;j++){
a[i][j]=x*c[i][j]%mod;
}
}
a[x+][x]=a[x+][x+]=;
}
Matrix operator +(Matrix b) {
Matrix c;
c.n = n;
for (int i = ; i < n; i++)
for (int j = ; j < n; j++)
c.a[i][j] = (a[i][j] + b.a[i][j]) % mod;
return c;
} Matrix operator +(int x) {
Matrix c = *this;
for (int i = ; i < n; i++)
c.a[i][i] += x;
return c;
} Matrix operator *(Matrix b)
{
Matrix p;
p.n = b.n;
memset(p.a,,sizeof p.a);
for (int i = ; i < n; i++)
for (int j = ; j < n; j++)
for (int k = ; k < n; k++)
p.a[i][j] = (p.a[i][j] + (a[i][k]*b.a[k][j])%mod) % mod;
return p;
} Matrix power(int t) {
Matrix ans,p = *this;
ans.n = p.n;
memset(ans.a,,sizeof ans.a);
for(int i=;i<=n;i++){//初始化ans
ans.a[i][i]=;
}
while (t) {
if (t & )
ans=ans*p;
p = p*p;
t >>= ;
}
return ans;
}
}init;
void Init(){//求组合数
memset(c,,sizeof c);
for(ll i=;i<=x;i++)
c[i][]=c[i][i]=;
for(ll i=;i<=x;i++)
for(ll j=;j<i;j++)
c[i][j]=((ll)c[i-][j-]+c[i-][j])%mod;
unit=;
}
/********************************矩阵模板**********************************/
int main(){
// freopen("in.txt","r",stdin);
while(scanf("%lld%lld%lld",&n,&x,&mod)!=EOF&&(n>,x>,mod>)){
Init();// #ok // for(int i=0;i<=x;i++){
// for(int j=0;j<=x;j++){
// cout<<c[i][j]<<" ";
// }
// cout<<endl;
// }
// cout<<endl; init.n=x+;
// cout<<"ok"<<endl;
init.init(); // for(int i=0;i<=x+1;i++){
// for(int j=0;j<=x+1;j++){
// cout<<init.a[i][j]<<" ";
// }cout<<endl;
// } // cout<<"ok"<<endl;
init=init.power(n); // for(int i=0;i<=x+1;i++){
// for(int j=0;j<=x+1;j++){
// cout<<init.a[i][j]<<" ";
// }cout<<endl;
// } for(int i=;i<=x;i++){
unit+=( (x*init.a[x+][i])%mod );
}
printf("%lld\n",(unit+mod)%mod);
}
return ;
}
A Very Simple Problem的更多相关文章
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- POJ 3468 A Simple Problem with Integers(线段树/区间更新)
题目链接: 传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Description Yo ...
- poj 3468:A Simple Problem with Integers(线段树,区间修改求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 58269 ...
- ACM: A Simple Problem with Integers 解题报告-线段树
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %l ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...
- BZOJ-3212 Pku3468 A Simple Problem with Integers 裸线段树区间维护查询
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1278 Sol ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92632 ...
- A Simple Problem with Integers(树状数组HDU4267)
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- A Simple Problem with Integers
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 77964 Acc ...
随机推荐
- JS中this到底指哪个对象
忘了原作者是谁了 自己也添加了一些东西 勉强可以观看一下 元素就不写了 可以自己添一下试试 先看这段代码 target.onclick = function(){ console.log(t ...
- day16<集合框架+>
集合框架(去除ArrayList中重复字符串元素方式) 集合框架(去除ArrayList中重复自定义对象元素) 集合框架(LinkedList的特有功能) 集合框架(栈和队列数据结构) 集合框架(用L ...
- 使用 Dawn 构建 React 项目
开发一个 React 项目,通常避免不了要去配置 Webpack 和 babel 之类,以支持 commonjs 或 es 模块及各种 es 新语法,及及进行 jsx 语法的转义.当然也可以用 cre ...
- java集合系列——List集合之Vector介绍(四)
1. Vector的简介 JDK1.7.0_79版本 Vector 类可以实现可增长的对象数组.与数组一样,它包含可以使用整数索引进行访问的组件.但是,Vector 的大小可以根据需要增大或缩小,以适 ...
- 用 Python 撸一个区块链
本文翻译自 Daniel van Flymen 的文章 Learn Blockchains by Building One 略有删改.原文地址:https://hackernoon.com/learn ...
- hud 2577 How to Type
How to Type Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- jQuery点击按钮实现div的隐藏和显示切换效果
<script type="text/javascript"> $(function(){ $('#click_event').click(function(){ i ...
- Socket简单实现数据交互及上传
声明:本文为原创,如需转载请说明出处:http://www.cnblogs.com/gudu1/p/7669175.html 首先为什么要写这个呢?因为在几个月之前我还使用Socket做一个小项目,而 ...
- Spring MVC Ajax 嵌套表单数据的提交
概述 在一些场景里,某个大表单里常常嵌套着一个或若干个小逻辑块,比如以下表单里"设计预审"中包括了一个子模块表单"拟定款项". 在这种情况下该怎么去设计实体类以 ...
- Bootstrap表格样式(附源码文件)--Bootstrap
1.表格默认样式 <h4>表格默认样式</h4><table><!--默认样式--> <tr><th>序号</th> ...