UVALive 6472 Powers of Pascal

题目的意思是:

给了一个无穷大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

时间: 2024-08-11 05:33:09

UVALive 6472 Powers of Pascal的相关文章

(leetcode题解)Pascal&#39;s Triangle

Pascal's Triangle  Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

使用RemObjects Pascal Script (转)

http://www.cnblogs.com/MaxWoods/p/3304954.html 摘自RemObjects Wiki 本文提供RemObjects Pascal Script的整体概要并演示如何创建一些简单的脚本. Pascal Script包括两个不同部分: 编译器 (uPSCompiler.pas) 运行时 (uPSRuntime.pas) 两部分彼此独立.可以分开使用,或通过TPSScript 控件使用他们,这个控件定义在uPSComponent.pas单元,对这两个部分进行简

金矿模型看动归(PASCAL版)

原文地址:http://www.cnblogs.com/SDJL/archive/2008/08/22/1274312.html{PASCAL语言} 对于动态规划,每个刚接触的人都需要一段时间来理解,特别是第一次接触的时候总是想不通为什么这种方法可行,这篇文章就是为了帮助大家理解动态规划,并通过讲解基本的01背包问题来引导读者如何去思考动态规划.本文力求通俗易懂,无异性,不让读者感到迷惑,引导读者去思考,所以如果你在阅读中发现有不通顺的地方,让你产生错误理解的地方,让你难得读懂的地方,请跟贴指出

LeetCode (13) Pascal&#39;s Triangle (杨辉三角 )

题目描述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return 从第三行开始,每行除了最左边和最右边两个数为1,其他数字都是上一行中相邻两个数字之和.根据上述规则可以写出下面的代码: class Solution { public: vector<vector<int> > generateRow1() { vector<in

UVALive 4848 Tour Belt

F - Tour Belt Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 4848 Description Korea has many tourist attractions. One of them is an archipelago (Dadohae in Korean), a cluster of small islands sca

UVALive 6467 Strahler Order 拓扑排序

这题是今天下午BNU SUMMER TRAINING的C题 是队友给的解题思路,用拓扑排序然后就可以了 最后是3A 其中两次RE竟然是因为: scanf("%d",mm); ORZ 以后能用CIN还是CIN吧 QAQ 贴代码了: 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <iostre

pascal,c,c++使用大于longint(long)的整型的方式

(pascal: pascal输出不需要说明格式,而式若子运算过程中的值都在qword范围内,则结果输出无错误. 1 begin 2 writeln(100000*100000); 3 writeln(100000*10000 mod 100); 4 end. c: 若运算结果为long long 或 __int_64,要在式子前加上(long long) 或 (__int_64) 1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int

UVALive 7077 Little Zu Chongzhi&#39;s Triangles (有序序列和三角形的关系)

这个题……我上来就给读错了,我以为最后是一个三角形,一条边可以由多个小棒组成,所以想到了状态压缩各种各样的东西,最后成功了……结果发现样例过不了,三条黑线就在我的脑袋上挂着,改正了以后我发现N非常小,想到了回溯每个棍的分组,最多分5组,结果发现超时了……最大是5^12 =  244,140,625,厉害呢…… 后来想贪心,首先想暴力出所有可能的组合,结果发现替换问题是一个难题……最后T T ,我就断片了.. 等看了别人的办法以后,我才发现我忽视了三角形的特性,和把数据排序以后的特点. 如果数据从

Gym 100299C &amp;&amp; UVaLive 6582 Magical GCD (暴力+数论)

题意:给出一个长度在 100 000 以内的正整数序列,大小不超过 10^ 12.求一个连续子序列,使得在所有的连续子序列中, 它们的GCD值乘以它们的长度最大. 析:暴力枚举右端点,然后在枚举左端点时,我们对gcd相同的只保留一个,那就是左端点最小的那个,只有这样才能保证是最大,然后删掉没用的. UVaLive上的数据有问题,比赛时怎么也交不过,后来去别的oj交就过了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&qu