SDUT 3895 fireworks 山东第八届ACM大赛C题(组合数学(杨辉三角)+逆元)
2017-06-09 18:44
429 查看
题目地址:点击打开链接
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?
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).
在一个数轴上,有n个点有非0值,其余点都为0。
然后,点x的值可以用1秒时间,分别传给左边和右边的一个点。
问T秒后点w的值是多少。
【解析】:
非常类似于一个杨辉三角,思维稍一转变即可。
把每一秒的状态都写成一行,对应在数轴位置,一直往下写,有规律。
询问T秒的位置w时,一共需要从第一行往下走T行,其中一部分向左下走,一部分往右下走。设分别为left和right步。然后从第T行w位置往上回溯到第0行(初始状态)
只需要算一下每一个能到达w的点对w的贡献值即可。
1、假设全部走左路,把左路全排列,A(left,left),
再去重复,除以A(left,left)。(别忘了乘上该起始点的值)
2、假设走一条右路,左路减一。则总路数A(left+1,left+1)
去重复,除以A(left,left),除以A(1,1);
3、再给右路加一条,左路减一。重复此步骤,直到left没有了。
把上面每一个步骤的数据累加,即为answer。
(我的代码,思路应该没问题,但是暂时wrong,,,,仅供参考)
【代码】:#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int N=1e6+5;
inline ll inv(ll b){return b<=1?1:(mod-mod/b)*inv(mod%b)%mod;}
ll f
;
int n,T,w;
int main()
{
f[0]=1;
for(int i=1;i<N;i++)f[i]=f[i-1]*i%mod;
while(cin>>n>>T>>w) //T秒后w点的值
{
ll ans=0;
for(int i=0;i<n;i++){
ll x,c;
scanf("%lld%lld",&x,&c);
if((T-w+x)%2||c==0)continue; //没贡献
int L=w-x+(T-w+x)/2;
int R=T-L;
//cout<<" "<<L<<' '<<R<<endl;
if(L<0||R<0)continue;
ll res=f[T]*inv(f[L])%mod*inv(f[R])%mod;
res=res*c%mod;
ans=(ans+res)%mod;
}
cout<<ans<<endl;
}
}
fireworks
Time Limit: 1000MS Memory Limit: 65536KB[align=center]Submit Statistic Discuss[/align]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.Example Input
1 2 0 2 2 2 2 2 0 3 1 2
Example Output
2 3【题意】:
在一个数轴上,有n个点有非0值,其余点都为0。
然后,点x的值可以用1秒时间,分别传给左边和右边的一个点。
问T秒后点w的值是多少。
【解析】:
非常类似于一个杨辉三角,思维稍一转变即可。
把每一秒的状态都写成一行,对应在数轴位置,一直往下写,有规律。
询问T秒的位置w时,一共需要从第一行往下走T行,其中一部分向左下走,一部分往右下走。设分别为left和right步。然后从第T行w位置往上回溯到第0行(初始状态)
只需要算一下每一个能到达w的点对w的贡献值即可。
1、假设全部走左路,把左路全排列,A(left,left),
再去重复,除以A(left,left)。(别忘了乘上该起始点的值)
2、假设走一条右路,左路减一。则总路数A(left+1,left+1)
去重复,除以A(left,left),除以A(1,1);
3、再给右路加一条,左路减一。重复此步骤,直到left没有了。
把上面每一个步骤的数据累加,即为answer。
(我的代码,思路应该没问题,但是暂时wrong,,,,仅供参考)
【代码】:#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int N=1e6+5;
inline ll inv(ll b){return b<=1?1:(mod-mod/b)*inv(mod%b)%mod;}
ll f
;
int n,T,w;
int main()
{
f[0]=1;
for(int i=1;i<N;i++)f[i]=f[i-1]*i%mod;
while(cin>>n>>T>>w) //T秒后w点的值
{
ll ans=0;
for(int i=0;i<n;i++){
ll x,c;
scanf("%lld%lld",&x,&c);
if((T-w+x)%2||c==0)continue; //没贡献
int L=w-x+(T-w+x)/2;
int R=T-L;
//cout<<" "<<L<<' '<<R<<endl;
if(L<0||R<0)continue;
ll res=f[T]*inv(f[L])%mod*inv(f[R])%mod;
res=res*c%mod;
ans=(ans+res)%mod;
}
cout<<ans<<endl;
}
}
相关文章推荐
- SDUT 3896 HEX 山东第八届ACM大赛D题(组合数学)
- SDUT 3895 (山东省第八届ACM省赛C题) fireworks 杨辉三角+乘法逆元
- 2017年山东省第八届ACM大学生程序设计竞赛 C fireworks(sdut 3895) 逆元求组合数
- sdut 3895/第八届省赛C题 fireworks 杨辉三角 逆元求C(n,m)%mod
- 2017山东省第八届ACM省赛 fireworks(杨辉三角 + 逆元)
- fireworks 2017年山东省ACM省赛C题 SDUT 3895 (逆元求组合数)
- 第八届山东省赛 C sdut 3895 fireworks(组合数+逆元)
- 山东省第八届acm省赛D题HEX(组合数学)
- 2017山东省第八届ACM省赛 D. HEX(组合数学)
- Sdut 2164 Binomial Coeffcients (组合数学) (山东省ACM第二届省赛 D 题)
- 第八届山东acm省赛 C题 fireworks (组合数)
- Sdut 2164 Binomial Coeffcients (组合数学) (山东省ACM第二届省赛 D 题)
- 山东省第八届acm大赛 J题 (SDUT 3902)
- 山东省第八届acm大赛 G题 (SDUT 3899)
- Sdut 2164 Binomial Coeffcients (组合数学) (山东省ACM第二届省赛 D 题)
- SDUT 3893 威佐夫+nim博弈(第八届山东ACM省赛A题)
- 【组合数】省赛CD题 SDUT_3895_fireworks SDUT_3896_HEX
- 山东省第八届 ACM 省赛 fireworks (组合数+逆元)
- ACM学习历程—SNNUOJ 1116 A Simple Problem(递推 && 逆元 && 组合数学 && 快速幂)(2015陕西省大学生程序设计竞赛K题)
- 山东 acm D HEX 数学 求组合数