码迷,mamicode.com
首页 > 其他好文 > 详细

vijos - P1739计算系数 (多项式计算 + 杨辉三角形 + 高速幂)

时间:2017-06-24 11:22:40      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:输入   一个   z-index   ott   microsoft   inline   console   tom   tag   

P1739计算系数

描写叙述

给定一个多项式(ax + by)^k,请求出多项式展开后x^n * y^m项的系数。

格式

输入格式

共一行,包括5个整数,分别为a,b,k。n,m,每两个整数之间用一个空格隔开。

输出格式

输出共1行,包括一个整数,表示所求的系数。这个系数可能非常大。输出对10007取模后的结果

例子1

例子输入1[复制]

1 1 3 1 2

例子输出1[复制]

3

限制

1s

提示

对于30%的数据,有0 ≤ k ≤ 10。
对于50%的数据,有a = 1, b = 1;
对于100%的数据。有0 ≤ k ≤ 1000,0 ≤ n, m ≤ k,且n+m = k,0 ≤ a,b ≤ 1,000,000.

来源

NOIp2011提高组Day2第一题

这道题目,最開始有点懵。没办法,高中的仅仅是基本都忘得一干二净了,后来百科了一下,发现他妈的就是个简单的组合数

 C(k,m) * a^i*b^j(i + j == m)极为(a + b)^m中的第i项

所以直接用高速幂取模解决这个问题

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstdio>
#include <string>
#include <ctime>
using namespace std;
typedef long long LL;
const int MAXN = 1e3 + 5;
const int mod = 10007;
LL A[2][MAXN];
LL mod_pow(LL x,LL n,LL mod) {
    if(n == 0) return 1;
    LL ret = mod_pow(x * x % mod, n / 2, mod);
    if(n & 1) ret = ret * x % mod;
    return ret;
}
int main() {
    int a, b, k, n, m;
    int opt = 0;
    scanf("%d%d%d%d%d", &a, &b, &k, &n, &m);
    A[0][0] = 1,A[0][1] = 1;
    for(int i = 2; i <= k; i ++) {
        opt = !opt;
        A[opt][0] = 1;
        for(int j = 1; j < i; j ++) {
            A[opt][j] =(A[!opt][j - 1] + A[!opt][j]) % mod;
        }
        A[opt][i] = 1;
    }
    LL ans = mod_pow(a, n, mod) * mod_pow(b, m, mod) * A[opt][n] % mod;
    printf("%I64d\n",ans);
    return 0;
}


vijos - P1739计算系数 (多项式计算 + 杨辉三角形 + 高速幂)

标签:输入   一个   z-index   ott   microsoft   inline   console   tom   tag   

原文地址:http://www.cnblogs.com/ljbguanli/p/7072530.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!