杨辉三角+逆元 zoj 4006 牛客网 小白的式子

链接:https://www.nowcoder.com/acm/contest/73/B
来源:牛客网

已知 f[1][1]=1,f[i][j]=a*f[i-1][j]+b*f[i-1][j-1](i>=2,1<=j<=i)。
对于其他情况f[i][j]=0

有T组询问,每次给出a,b,n,m,求f[n][m] mod (998244353)

输入描述:

第一行为一个整数T,表示询问个数。
接下来一共T行,每行四个整数a,b,n,m。

输出描述:

一共T行,每行一个整数,表示f[n][m] mod (998244353)

示例1

输入

2
2 3 3 3
3 1 4 1

输出

9
27
//注意由上一层的该项和前一项递推得到,那定与杨辉三角相联系
//注意对答案取模时,乘一次,取一次,
//ll ans=C(n-1,m-1)*pow_(a,n-m)%P*pow_(b,m-1)%P;
#include<bits/stdc++.h>
using namespace std;
const int N=100010;
const long long P=998244353;
typedef long long ll;
ll i,fac[N],inv[N];
ll C(ll n,ll m){return fac[n]*inv[m]%P*inv[n-m]%P;}
ll pow_(ll a,ll b){
    ll ans=1,tmp=a%P;
    while(b){
        if(b&1) ans=ans*tmp%P;
        tmp=tmp*tmp%P;
        b/=2;
    }    
    return ans;
}
int main(){
    for(fac[0]=fac[1]=1,i=2;i<N;i++)fac[i]=fac[i-1]*i%P;
    inv[N-1]=pow_(fac[N-1],P-2);
    for(i=N-2;i>=0;i--)inv[i]=inv[i+1]*(i+1LL)%P;
    int t;ll a,b,n,m;scanf("%d",&t);
    while(t--){
        scanf("%lld%lld%lld%lld",&a,&b,&n,&m);
        ll ans=C(n-1,m-1)*pow_(a,n-m)%P*pow_(b,m-1)%P;
        printf("%lld\n",ans);
    }
    return 0;
}
View Code
Travel along the Line

Time Limit: 1 Second       Memory Limit: 65536 KB

BaoBao is traveling along a line with infinite length.

At the beginning of his trip, he is standing at position 0. At the beginning of each second, if he is standing at position , with  probability he will move to position , with  probability he will move to position , and with  probability he will stay at position . Positions can be positive, 0, or negative.

DreamGrid, BaoBao's best friend, is waiting for him at position . BaoBao would like to meet DreamGrid at position  after exactly  seconds. Please help BaoBao calculate the probability he can get to position  after exactly  seconds.

It's easy to show that the answer can be represented as , where  and  are coprime integers, and  is not divisible by . Please print the value of  modulo , where  is the multiplicative inverse of  modulo .

Input

There are multiple test cases. The first line of the input contains an integer  (about 10), indicating the number of test cases. For each test case:

The first and only line contains two integers  and  (). Their meanings are described above.

Output

For each test case output one integer, indicating the answer.

Sample Input
3
2 -2
0 0
0 1
Sample Output
562500004
1
0

Author: YE, Zicheng
Source: ZOJ Monthly, March 2018

该题即为杨辉三角的变形,因右边的一项是和左边的一项相等,即可转换成为杨辉三角的形式;

 

转载于:https://www.cnblogs.com/vainglory/p/8549756.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值