山东省第八届ACM省赛 fireworks(杨辉三角+组合数)

13 篇文章 0 订阅
13 篇文章 0 订阅

Problem Description

Hmz likes to play fireworks, especially when they are put regularly.
Now he puts some fireworks in a line. This time he put a trigger on each firework. With that trigger, each firework will explode and split into two parts per second, which means if a firework is currently in position x, then in next second one part will be in position x−1 and one in x+1. They can continue spliting without limits, as Hmz likes.
Now there are n fireworks on the number axis. Hmz wants to know after T seconds, how many fireworks are there in position w?
这里写图片描述

Input

Input contains multiple test cases.
For each test case:
The first line contains 3 integers n,T,w(n,T,|w|≤10^5)
In next n lines, each line contains two integers xi and ci, indicating there are ci fireworks in position xi at the beginning(ci,|xi|≤10^5).

Output

For each test case, you should output the answer MOD 1000000007.

Sample Input

1 2 0
2 2
2 2 2
0 3
1 2

Sample Output

2
3

题目描述

最初有n个火源,xi、ci分别表示每个火源的位置和包含的烟花个数.每经过一秒,x位置的烟花会扩散到x-1和x+1两个位置,问T秒之后,w 位置由多少个烟花.

解题思路

假设现在在x位置存在 1 个烟花,则每经过一秒烟花的扩散位置为:
t=0 0 0 0 0 0 0 1 0 0 0 0 0 0 | 0 0 0 0 0 0 C00 C 0 0 0 0 0 0 0 0
t=1 0 0 0 0 0 1 0 1 0 0 0 0 0 | 0 0 0 0 0 C01 C 1 0 0 C11 C 1 1 0 0 0 0 0
t=2 0 0 0 0 1 0 2 0 1 0 0 0 0 | 0 0 0 0 C02 C 2 0 0 C12 C 2 1 0 C22 C 2 2 0 0 0 0
t=3 0 0 0 1 0 3 0 3 0 1 0 0 0 | 0 0 0 C03 C 3 0 0 C13 C 3 1 0 C23 C 3 2 0 C33 C 3 3 0 0 0
t=4 0 0 1 0 4 0 6 0 4 0 1 0 0 | 0 0 C04 C 4 0 0 C14 C 4 1 0 C24 C 4 2 0 C34 C 4 3 0 C44 C 4 4 0 0
……
可以发现是杨辉三角只是中间填充了0,则借助组合数 Cmn C n m 来求解,显然n=t,由于中间填充了0,所以 m=t(xw)2 m = t − ( x − w ) 2 ,且只有t-(x-w)与 t 同奇偶时才有贡献值,否则该位置值为0.

代码实现

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define IO ios::sync_with_stdio(false);\
cin.tie(0);\
cout.tie(0);
const int mod = 1e9+7;
const int maxn = 1e5+7;
ll inv0[maxn*2],mul[maxn*2];
void init()
{
    mul[0]=1;
    for(int i=1;i<maxn;i++)
        mul[i]=(mul[i-1]*i)%mod;
}
void get_inv()
{
    inv0[0]=inv0[1]=1;
    for(int i=2;i<maxn;i++)
        inv0[i] = ((-mod /i) * inv0[mod % i]%mod+mod)%mod;
    for(int i=2;i<maxn;i++)
        inv0[i] = inv0[i-1] * inv0[i] % mod;
}
int calc(ll n,ll m)
{
    return mul[n]*inv0[m]%mod*inv0[n-m]%mod;
}
int main()
{
    IO;
    ll n,t,w;
    init();
    get_inv();
    while(cin>>n>>t>>w)
    {
        ll ans=0;
        int xi;
        ll ci;
        for(int i=0;i<n;i++)
        {
            cin>>xi>>ci;
            if((t-abs(xi-w)<0)||(t-abs(xi-w))%2!=0) continue;
            ans=(ans+ci*calc(t,(t-abs(xi-w))/2)%mod)%mod;
        }
        cout<<ans<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值