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. String和StringBuffer分别作为参数传递注意项

    public staticvoid main(){ String s1 = "abc"; StringBuffer sb = new StringBuffer(); sb.appe ...

  2. global,local,static的区别

    1.在函数内部使用global关键字定义的变量可以成为全局变量,如果该变量已经被定义了,那么他的值就是原来的值,否则就是一个新的全局变量(一句话:已存在就不再创建): <?php $a=1; f ...

  3. OC Block网上转载

    1.block是一个特殊的OC对象, 它建立在栈上, 而不是堆上, 这么做一个是为性能考虑,还有就是方便访问局部变量. 2.默认情况下block使用到的局部变量都会被复制,而不是保留.所以它无法改变局 ...

  4. AngularJS 1.3中的一次性数据绑定(one-time bindings)

    点击查看AngularJS系列目录 谈谈AngularJS 1.3中的一次性数据绑定(one-time bindings) 不久之前,AngularJS 1.3版本正式发布,其中添加了很多的性特性,同 ...

  5. uva 10391

    这个题,单纯做出来有很多种方法,但是时间限制3000ms,因此被TL了不知道多少次,关键还是找对最优解决方法,代码附上: #include<bits/stdc++.h> using nam ...

  6. Quartz学习——Spring和Quartz集成详解(三)

    Spring是一个很优秀的框架,它无缝的集成了Quartz,简单方便的让企业级应用更好的使用Quartz进行任务的调度.下面就对Spring集成Quartz进行简单的介绍和示例讲解!和上一节 Quar ...

  7. DAO与DTO

    DAO叫数据访问对象(data access object) DTO是数据传输对象(data transfer object) DAO通常是将非对象数据(如关系数据库中的数据)以对象的方式操纵.(即一 ...

  8. BZOJ 1041 [HAOI2008]圆上的整点:数学

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1041 题意: 给定n(n <= 2*10^9),问你在圆x^2 + y^2 = n^ ...

  9. SqlServer与Linq 无限递归目录树且输出层级

    ALTER VIEW [dbo].[view_TreeLevel] AS WITH cte AS ( SELECT a.ModuleID , a.Module_Name , a.Module_Desc ...

  10. 数据库表反向生成(一) MyBatis-generator与IDEA的集成

    在Dao层,通过数据库表反向生成,可以节省我们很多的精力,把更多的精力投入复杂的业务中. 数据库表反向生成,指的是通过数据库如mysql中的库表schema生成dao层读写表的基础代码,包括model ...