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
3 4 1000
-1 -1 -1
 
Sample Output
5050
444
 
 
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的更多相关文章

  1. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  2. POJ 3468 A Simple Problem with Integers(线段树/区间更新)

    题目链接: 传送门 A Simple Problem with Integers Time Limit: 5000MS     Memory Limit: 131072K Description Yo ...

  3. poj 3468:A Simple Problem with Integers(线段树,区间修改求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 58269   ...

  4. ACM: A Simple Problem with Integers 解题报告-线段树

    A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %l ...

  5. poj3468 A Simple Problem with Integers (线段树区间最大值)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92127   ...

  6. POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)

    A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...

  7. 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 ...

  8. POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92632   ...

  9. A Simple Problem with Integers(树状数组HDU4267)

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...

  10. A Simple Problem with Integers

    A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 77964 Acc ...

随机推荐

  1. angularjs的几种常见写法

    学习angularjs不久,遇见的angularjs的写法也是很多的感觉,今天就在这里记录一下,还有没见过的,继续学习中... angularjs 常用的几种种写法 1.链式: angular.mod ...

  2. Storm同步调用之DRPC模型探讨

    摘要:Storm的编程模型是一个有向无环图,决定了storm的spout接收到外部系统的请求后,spout并不能得到bolt的处理结果并将结果返回给外部请求.所以也就决定了storm无法提供对外部系统 ...

  3. Redis——windows环境安装redis和redis sentinel部署

    一:Redis的下载和安装 1:下载Redis Redis的官方网站Download页面,Redis提示说:Redis的正式版不支持Windows,要Windows学习Redis,请点击Learn m ...

  4. C++ Primer Plus 6 第一章

    一.机器语言.汇编语言.C\C++.高级语言 机器语言:机器真正识别,能在机器上运行的语言. 汇编语言:低级语言,直接操作硬件,如直接访问cpu寄存器和内存单元.不具有移植性.因为不同的平台对应的硬件 ...

  5. 设计模式学习之“观察者模式” [C#]

    <深入浅出设计模式>学习笔记第二章 需求: 开发一套气象监测应用,如图: 气象站,目前有三种装置,温度.湿度和气压感应装置. WeatherData对象追踪气象站的数据,并更新到布告板,布 ...

  6. Asp数据转Json

    需要引用的文件: json.asp(可在JSON官网下载,也可在底部链接的demo中直接拷贝该文件) Conn.asp是链接数据库文件 <%@LANGUAGE="%> <% ...

  7. 【Windows 10 应用开发】细说文本资源文件(resw)

    最近,小戏骨版<红楼梦>很是火热,老周一口气看完了9集,一直看到 Surface 的风扇呼呼响.林黛玉和薛宝钗这两个角色都演得不怎么样,倒是演史湘云的娃娃演得不错,老周甚是喜欢. 于是,昨 ...

  8. Python自学笔记-map和reduce函数(来自廖雪峰的官网Python3)

    感觉廖雪峰的官网http://www.liaoxuefeng.com/里面的教程不错,所以学习一下,把需要复习的摘抄一下. 以下内容主要为了自己复习用,详细内容请登录廖雪峰的官网查看. Python内 ...

  9. ubuntu命令查询版本和内核版本

    1.查看ubuntu版本号: 方法一: cat  /etc/issue 返回结果: Ubuntu 6.06.2 LTS \n \l   方法二: sudo lsb_release -a  返回结果: ...

  10. Quartz.NET实现作业调度

    一.Quartz.NET介绍 Quartz.NET是一个强大.开源.轻量的作业调度框架,是 OpenSymphony 的 Quartz API 的.NET移植,用C#改写,可用于winform和asp ...