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 ...
随机推荐
- 基于c编写的关于随机生成四则运算的小程序
基于http://www.cnblogs.com/HAOZHE/p/5276763.html改编写的关于随机生成四则运算的小程序 github源码和工程文件地址:https://github.com/ ...
- C#操作SqlServer MySql Oracle通用帮助类Db_Helper_DG(默认支持数据库读写分离、查询结果实体映射ORM)
[前言] 作为一款成熟的面向对象高级编程语言,C#在ADO.Net的支持上已然是做的很成熟,我们可以方便地调用ADO.Net操作各类关系型数据库,在使用了多年的Sql_Helper_DG后,由于项目需 ...
- macbook 263企业邮箱设置
第一步:打开邮箱,点击添加账号,选择其他 第二步:填写完整的电子邮件地址和密码 第三步:填写收件服务器(popcom.263xmail.com),发件服务器(smtpcom.263xmail.com)
- PyTorch教程之Neural Networks
我们可以通过torch.nn package构建神经网络. 现在我们已经了解了autograd,nn基于autograd来定义模型并对他们有所区分. 一个 nn.Module模块由如下部分构成:若干层 ...
- 说下browserslist
browserslist 是一个开源项目 见到有些package.json里会有如下的配置参数 "browserslist": [ "> 1%", &qu ...
- Maven在Windows中的配置以及IDE中的项目创建
Maven在Windows下的配置 1.Maven下载地址:http://maven.apache.org/download.cgi,下载红框里的版本即可. 2.解压到D盘: 3.修改配置文件sett ...
- 网页meta标签总结
文章摘抄自网络. 参考文章:http://www.cnblogs.com/lpt1229/p/5628631.html http://blog.csdn.net/aiolos1111/article/ ...
- 简易RPC框架-心跳与重连机制
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- 初始Socket编程(python)
通信双方要有一个服务端和一个客户端,所以要分开去写代码. 所以我创建了两个py程序,第一个是服务端:iServer.py 和客户端 iClient.py 服务端: #coding:utf-8from ...
- Python自学笔记-Django分页器小实例
from django.core.paginator import Paginator iter = 'abcdefhijklmnopqw' paginator = Paginator(iter,4) ...