[ARC058E]E - Iroha and Haiku题解
Time Limit: 4 sec / Memory Limit: 512 MiB
Score : 700 points
Problem Statement
Haiku is a short form of Japanese poetry. A Haiku consists of three phrases with 5, 7 and 5 syllables, in this order.
Iroha is looking for X,Y,Z-Haiku (defined below) in integer sequences.
Consider all integer sequences of length N whose elements are between 1 and 10, inclusive. Out of those 10N sequences, how many contain an X,Y,Z-Haiku?
Here, an integer sequence a0,a1,...,aN−1 is said to contain an X,Y,Z-Haiku if and only if there exist four indices x,y,z,w(0≦x<y<z<w≦N) such that all of the following are satisfied:
- ax+ax+1+...+ay−1=X
- ay+ay+1+...+az−1=Y
- az+az+1+...+aw−1=Z
Since the answer can be extremely large, print the number modulo 109+7.
有道 翻译
问题陈述
俳句是日本诗歌的一种简短形式。俳句由五个音节、七个音节和五个音节组成,顺序如下:_
Iroha正在寻找整数序列中的_ X,Y,Z -Haiku_(定义如下)。
考虑所有长度为 N 且元素在 1 和 10 之间的整数序列。在这些 10N 序列中,有多少包含 X,Y,Z -俳句?
在这里,一个整数序列 a0,a1,...,aN−1 被认为包含 X,Y,Z -Haiku_当且仅当存在四个索引 x,y,z,w(0≦x<y<z<w≦N) 并且满足以下所有条件:
—— ax+ax+1+...+ay−1=X
—— ay+ay+1+...+az−1=Y
—— az+az+1+...+aw−1=Z
因为答案可能非常大,所以打印出模数 109+7 。
Constraints
- 3≦N≦40
- 1≦X≦5
- 1≦Y≦7
- 1≦Z≦5
Input
The input is given from Standard Input in the following format:
N X Y Z
有道 翻译
# #输入
输入来自标准输入,格式如下:
N X Y Z
Output
Print the number of the sequences that contain an X,Y,Z-Haiku, modulo 109+7.
有道 翻译
# #输出
打印包含 X,Y,Z -俳句,模 109+7 的序列的个数。
Sample Input 1
Copy
3 5 7 5
Sample Output 1
Copy
1
Here, the only sequence that contains a 5,7,5-Haiku is [5,7,5].
有道 翻译
###输出示例
1
这里,包含 5,7,5 -俳句的唯一序列是 [5,7,5] 。
Sample Input 2
Copy
4 5 7 5
Sample Output 2
Copy
34
Sample Input 3
Copy
37 4 2 3
Sample Output 3
Copy
863912418
Sample Input 4
Copy
40 5 7 5
Sample Output 4
Copy
562805100
思路
DP。
代码见下
#include<bits/stdc++.h>
using namespace std;
long long n,x,y,z,ma,f[41][150005],op,lk=1,mod=998244353;
int main(){
cin>>n>>x>>y>>z;
ma=1<<(x+y+z);
f[0][0]=1;
for(int i=0;i<=n-1;i++){
for(int j=0;j<=ma-1;j++){
for(int k=1;k<=10;k++){
op=((j<<k)+(1<<(k-1)))&(ma-1);
if((op&(1<<(z-1)))&&(op&(1<<(y+z-1)))&&(op&(1<<(x+y+z-1)))){
}
else{
f[i+1][op]=(f[i+1][op]+f[i][j])%mod;
}
}
}
}
for(int i=1;i<=n;i++){
lk*=10;
lk%=mod;
}
for(int i=0;i<=ma-1;i++){
lk-=f[n][i];
lk%=mod;
}
cout<<(lk+mod)%998244353ll<<endl;
return 0;
}

浙公网安备 33010602011771号