UVALive 6472 Powers of Pascal
2014-07-18 11:09
288 查看
题目的意思是:
给了一个无穷大Pascal矩阵,定义了powers,然后询问power为P的pascal矩阵里面的第R行C列的元素是多少。
最开始读错题意了...然后 就成了一个神得不得了的题了。后来请教的别人。
感觉可以用矩阵快速幂做。
然后,不用快速幂的话,你会惊奇的发现,变成了找规律的题了...
答案成了 comb(i,j) * P^(i-j)
对于comb(i,j),利用组合数性质,可以得到,当j>i-j 的时候 j=i-j;
因为题目说答案不会爆long long 所以可以预处理,大概56左右,后发现50也行
组合数公式 Comb(i,j)=Comb(i-1,j)+Comb(i-1,j-1);
Ps:写这个是为了提醒自己....有时候打表找规律也是一种做一些数学有关的题的手段,当时实在不知道怎么做了时。
代码如下,再次感谢xxx
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100000;
typedef long long LL;
LL C[maxn+2][52];
LL kk,k,p,r,c;
void init()
{
for(int i=0;i<=maxn;i++) C[i][0]=1;
for(int i=1;i<=maxn;i++)
for(int j=1;j<=min(i,50);j++)
C[i][j]=C[i-1][j]+C[i-1][j-1];
}
int main()
{
init();
//cout<<C[3][1]<<endl;
scanf("%lld",&k);
while(k--)
{
scanf("%lld%lld%lld%lld",&kk,&p,&r,&c);
LL ans=1;
for(int i=1;i<=(r-c);i++)
ans*=p;
if(c>r/2) c=r-c;
ans*=C[r][c];
printf("%lld %lld\n",kk,ans);
}
return 0;
}
给了一个无穷大Pascal矩阵,定义了powers,然后询问power为P的pascal矩阵里面的第R行C列的元素是多少。
最开始读错题意了...然后 就成了一个神得不得了的题了。后来请教的别人。
感觉可以用矩阵快速幂做。
然后,不用快速幂的话,你会惊奇的发现,变成了找规律的题了...
答案成了 comb(i,j) * P^(i-j)
对于comb(i,j),利用组合数性质,可以得到,当j>i-j 的时候 j=i-j;
因为题目说答案不会爆long long 所以可以预处理,大概56左右,后发现50也行
组合数公式 Comb(i,j)=Comb(i-1,j)+Comb(i-1,j-1);
Ps:写这个是为了提醒自己....有时候打表找规律也是一种做一些数学有关的题的手段,当时实在不知道怎么做了时。
代码如下,再次感谢xxx
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100000;
typedef long long LL;
LL C[maxn+2][52];
LL kk,k,p,r,c;
void init()
{
for(int i=0;i<=maxn;i++) C[i][0]=1;
for(int i=1;i<=maxn;i++)
for(int j=1;j<=min(i,50);j++)
C[i][j]=C[i-1][j]+C[i-1][j-1];
}
int main()
{
init();
//cout<<C[3][1]<<endl;
scanf("%lld",&k);
while(k--)
{
scanf("%lld%lld%lld%lld",&kk,&p,&r,&c);
LL ans=1;
for(int i=1;i<=(r-c);i++)
ans*=p;
if(c>r/2) c=r-c;
ans*=C[r][c];
printf("%lld %lld\n",kk,ans);
}
return 0;
}
相关文章推荐
- UVALive 6472 Powers of Pascal
- UVALive2953 POJ1775 ZOJ2358 Sum of Factorials【打表+暴力+水题】
- UVA 766 Sum of powers
- UVALive-7278 - Game of Cards(sg函数)
- UVALive3399 UVA1210 POJ2739 Sum of Consecutive Prime Numbers【素数筛选+尺取法】
- UVALive 7392 Bundles of Joy【bitset】【类树形DP】【杂题】
- UVALive - 6856 Circle of digits 后缀数组+二分
- UVALive 7340 Sum of MSLCM
- UVAlive 6800 The Mountain of Gold?(负环判断)
- UVALive - 3972 March of the Penguins(最大流+枚举)
- UVaLive/LA 6800 The Mountain of Gold?(BellmanFord判负环+记忆化搜索)
- UVALive - 3523 Knights of the Round Table(【点双连通分量】+【二分图判定】)
- UVALive 5783 Everyone out of the Pool
- UVaLive 7359 Sum Kind Of Problem (数学,水题)
- UVAlive3523 Knights of the Round Table(bcc)
- Guardian of Decency UVALive - 3415 最大独立集=结点数-最大匹配数 老师带大学生旅游
- uvalive 6800 - The Mountain of Gold? 判负环
- POJ1060 HDU1343 ZOJ1026 UVALive2323 Modular multiplication of polynomials(多项式模2除)
- UVALive 6342 The Mirror of Galadriel (回文串)
- UVALIVE 2927 "Shortest" pair of paths