Time limit : 2sec / Memory limit : 1024MB
Score : 700 points
Problem Statement
Takahashi has a tower which is divided into N layers.Initially, all the layers are uncolored. Takahashi is going to paint some of the layers in red, green or blue to make a beautiful tower.He defines the beauty of the tower as follows:
- The beauty of the tower is the sum of the scores of the N layers, where the score of a layer is A if the layer is painted red, A+B if the layer is painted green, B if the layer is painted blue, and 0 if the layer is uncolored.
Here, A and B are positive integer constants given beforehand. Also note that a layer may not be painted in two or more colors.
Takahashi is planning to paint the tower so that the beauty of the tower becomes exactly K.How many such ways are there to paint the tower? Find the count modulo 998244353.Two ways to paint the tower are considered different when there exists a layer that is painted in different colors, or a layer that is painted in some color in one of the ways and not in the other.
Constraints
- 1≤N≤3×105
- 1≤A,B≤3×105
- 0≤K≤18×1010
- All values in the input are integers.
Input
Input is given from Standard Input in the following format:
N A B K
Output
Print the number of the ways to paint tiles, modulo 998244353.
Sample Input 1
4 1 2 5
Sample Output 1
40
In this case, a red layer worth 1 points, a green layer worth 3 points and the blue layer worth 2 points. The beauty of the tower is 5 when we have one of the following sets of painted layers:
- 1 green, 1 blue
- 1 red, 2 blues
- 2 reds, 1 green
- 3 reds, 1 blue
The total number of the ways to produce them is 40.
Sample Input 2
2 5 6 0
Sample Output 2
1
The beauty of the tower is 0 only when all the layers are uncolored. Thus, the answer is 1.
Sample Input 3
90081 33447 90629 6391049189
Sample Output 3
577742975
题意:给一个n层塔涂色,红色,绿色,蓝色的分数各不相同,绿色的分数等于红色和蓝色分数和,求有多少种方法可以使塔的涂色的分数等于k。
题解:绿色等于红色和蓝色分数和,因此只需要考虑红色和蓝色的涂法,绿色就可以看作红色和蓝色一起涂在同一层。即A * a + B * b = k。对每一个a,b,ans += C(n,a) * C(n,b)。
想是比较好想的,但是我觉得很难写,可能是我的模运算不怎么好吧。主要是预处理阶乘的逆元这一个方法,不知道怎么写,是看着网上的方法写的。这个方法要学会。
#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const int maxn = 3e5 + 100;
long long fac[maxn],inv[maxn];
long long q_pow(long long x,long long n)
{
long long res = 1;
while(n > 0)
{
if(n & 1) res = res * x % mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
void inti()
{
int i;
fac[0] = 1;
for(i = 1;i < maxn;i++)
{
fac[i] = (fac[i - 1] * i) % mod;
}
inv[maxn - 1] = q_pow(fac[maxn - 1],mod - 2);
for(i = maxn - 2;i >= 0;i--)
{
inv[i] = inv[i + 1] * (i + 1) % mod;
}
}
long long C(int n,int m)
{
if(n < m) return 0;
return fac[n] * inv[m] % mod * inv[n - m] % mod;
}
int main()
{
long long n,a,b,k,A,B,ans;
inti();
scanf("%lld%lld%lld%lld",&n,&A,&B,&k);
ans = 0;
for(a = 0; a <= n; a++)
{
if(k - A * a < 0) continue;
if((k - A * a) % B) continue;
b = (k - A * a) / B;
if(b > n) continue;
ans = (ans + C(n,a) * C(n,b) + mod) % mod;
}
printf("%lld\n",ans);
return 0;
}