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
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
const int mod=1e9+7;
struct Node{
ll pos,val;
bool operator<(const Node& b)const{
return pos<b.pos;
}
}p[maxn];
ll c[maxn];
ll NY(ll base,int n){
ll ans=1;
while(n){
if(n&1)
ans=(ans*base)%mod;
base=(base*base)%mod;
n/=2;
}
return ans;
}
void get_ans(int t){
c[0]=1;
for(int i=1;i<=t;i++){
c[i]=(c[i-1]*(t-i+1)%mod*NY((ll)i,mod-2))%mod;
}
}
ll solve(ll t,int id,ll dif){
ll m=(t-dif)/2;
return (c[m]*p[id].val)%mod;
}
int is_ok(int a,int b){
if(a%2==0&&b%2!=0)return 1;
if(a%2!=0&&b%2==0)return 1;
return 0;
}
int main()
{
int n,t,w;
while(scanf("%d %d %d",&n,&t,&w)==3){
for(int i=0;i<n;i++){
scanf("%lld %lld",&p[i].pos,&p[i].val);
}
get_ans(t);
sort(p,p+n);
ll ans=0;
for(int i=0;i<n;i++){
int tmp=abs(p[i].pos-w);
if(tmp>t||is_ok(tmp,t))continue;
ans=(ans+solve((ll)t,i,(ll)tmp))%mod;
}
printf("%lld\n",ans);
}
return 0;
}
/***************************************************
User name: o
Result: Accepted
Take time: 488ms
Take Memory: 2300KB
Submit time: 2018-05-04 18:55:16
****************************************************/
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
const int mod=1e9+7;
struct Node{
ll pos,val;
bool operator<(const Node& b)const{
return pos<b.pos;
}
}p[maxn];
ll f[maxn];
void init(){
f[1]=f[0]=1;
for(int i=2;i<maxn;i++){
f[i]=(f[i-1]*i)%mod;
}
}
ll get_niyuan(ll base,int n){
ll ans=1;
while(n){
if(n&1){
ans=(ans*base)%mod;
}
base=(base*base)%mod;
n/=2;
}
return ans;
}
ll C(ll n,ll k){
ll ans=(f[k]*f[n-k])%mod;
ans=get_niyuan(ans,mod-2);
return (ans*f[n])%mod;
}
ll Lucas(int n,int m){
return m==0?1:(C(n%mod,m%mod)*Lucas(n/mod,m/mod))%mod;
}
ll solve(ll t,int id,ll dif){
ll m=(t-dif)/2;
return (Lucas(t,m)*p[id].val)%mod;
}
int is_ok(int a,int b){
if(a%2==0&&b%2!=0)return 1;
if(a%2!=0&&b%2==0)return 1;
return 0;
}
int main()
{
int n,t,w;
init();
while(scanf("%d %d %d",&n,&t,&w)==3){
for(int i=0;i<n;i++){
scanf("%lld %lld",&p[i].pos,&p[i].val);
}
ll ans=0;
for(int i=0;i<n;i++){
int tmp=abs(p[i].pos-w);
if(tmp>t||(is_ok(tmp,t)))continue;
ans=(ans+solve((ll)t,i,(ll)tmp))%mod;
}
printf("%lld\n",ans);
}
return 0;
}
/***************************************************
User name: o
Result: Accepted
Take time: 392ms
Take Memory: 2444KB
Submit time: 2018-05-04 15:41:29
****************************************************/