距离省赛已经过去了两个多月了,然而还有一道题卡在心里迟迟没有解决,之前的博客也已经扯过了,C fireworks,还是先把题目贴出来。
fireworks
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 positionx, then in next second one part will be in position x−1 and one inx+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.
Example Input
1 2 0 2 2 2 2 2 0 3 1 2
Example Output
2 3
题意是说已知有n个烟花,这些烟花会每秒变为两个,在坐标上左右再出现两个,问经过T秒后,在w位置上有多少烟花。下面n行分别表示在x位置上有c个烟花。
在纸上简单画一下,不难发现这就是一个杨辉三角,对于求杨辉三角,这道题数据很大打表是肯定不行的,所以就要直接求组合数。当时比赛时我只了解过Lucas定理,而且之前一直都用的Lucas定理求,所以没太在意过,结果在比赛的时候一直到最后还是只得到了一堆TLE。
现在再去看,才发现,除了Lucas之外,还有另一个方法直接求出组合数,求逆元。这个方法适用于数较小但mod较大的情况,这题mod为1000000007,所以这个题目的本意是让用求逆元的方法来做,无奈当时的我并不知道有这个方法。
现在终于把这道心头上挂着的题做了,就像曾经说的,果然“差了一个Lucas的距离”,恩,就这样吧。
具体的求组合数方法还是另开一贴单独整理出来吧,下面AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const long long mod=1000000007;
const long long fcnt=100005;
long long fac[100005];
int getfac()
{
int i;
fac[0]=1;
for(i=1;i<fcnt;i++)
{
fac[i]=fac[i-1]*i%mod;
}
return 0;
}
long long Pow(long long a,long long b)
{
long long ans=1;
while(b)
{
if(b&1)
{
b--;
ans=(ans*a)%mod;
}
b>>=1;
a=(a*a)%mod;
}
return ans;
}
long long inv(long long a)
{
return Pow(a,mod-2);
}
long long C(long long n,long long m)
{
if(n<m)
return 0;
return fac[n]*inv(fac[m])%mod*inv(fac[n-m])%mod;
}
int main()
{
long long n,T,w;
int i;
long long x,c;
long long ans;
getfac();
while(scanf("%lld%lld%lld",&n,&T,&w)!=EOF)
{
ans=0;
for(i=1;i<=n;i++)
{
scanf("%lld%lld",&x,&c);
if(abs(x-w)%2==T%2&&abs(x-w)<=T)
{
ans=(ans+(c*C(T,abs(T/2-(abs(x-w))/2)))%mod)%mod;
}
}
cout<<ans<<endl;
}
return 0;
}