E - 和風いろはちゃん / Iroha and Haiku(dp+状压)

博客围绕整数序列中寻找X,Y,Z - Haiku的问题展开。给定长度为N且元素在1到10之间的整数序列,需计算其中包含X,Y,Z - Haiku的序列数量,并对结果取模。同时给出了问题的约束条件、输入输出格式及示例,还提及了状态转移思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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,ZX,Y,Z-Haiku (defined below) in integer sequences.

Consider all integer sequences of length NN whose elements are between 11 and 1010, inclusive. Out of those 10N10N sequences, how many contain an X,Y,ZX,Y,Z-Haiku?

Here, an integer sequence a0,a1,...,aN−1a0,a1,...,aN−1 is said to contain an X,Y,ZX,Y,Z-Haiku if and only if there exist four indices x,y,z,w(0≦x<y<z<w≦N)x,y,z,w(0≦x<y<z<w≦N)such that all of the following are satisfied:

  • ax+ax+1+...+ay−1=Xax+ax+1+...+ay−1=X
  • ay+ay+1+...+az−1=Yay+ay+1+...+az−1=Y
  • az+az+1+...+aw−1=Zaz+az+1+...+aw−1=Z

Since the answer can be extremely large, print the number modulo 109+7109+7.

Constraints

  • 3≦N≦403≦N≦40
  • 1≦X≦51≦X≦5
  • 1≦Y≦71≦Y≦7
  • 1≦Z≦51≦Z≦5

Input

The input is given from Standard Input in the following format:

NN XX YY ZZ

Output

Print the number of the sequences that contain an X,Y,ZX,Y,Z-Haiku, modulo 109+7109+7.


Sample Input 1 Copy

Copy

3 5 7 5

Sample Output 1 Copy

Copy

1

Here, the only sequence that contains a 5,7,55,7,5-Haiku is [5,7,5][5,7,5].


Sample Input 2 Copy

Copy

4 5 7 5

Sample Output 2 Copy

Copy

34

Sample Input 3 Copy

Copy

37 4 2 3

Sample Output 3 Copy

Copy

863912418

Sample Input 4 Copy

Copy

40 5 7 5

Sample Output 4 Copy

Copy

562805100

题意:
给你N,X,Y,Z;

N->数的长度(N个数)每个数只能是(0-10),

问你有多少x,y,z,w。满足上面的要求。

d[i][s]表示考虑到第i位,第x+y+z为1然后后面的串为s的二进制表示的数。

转移时枚举第i+1位的情况即可

#include<bits/stdc++.h>
#define ll long long
const ll mod=1e9+7;
using namespace std;
ll d[45][1<<17];
ll n,x,y,z;
int main()
{
    scanf("%lld%lld%lld%lld",&n,&x,&y,&z);
    ll ans=1;
    for(ll i=1;i<=n;i++)//求出最多有多少种情况10^n。
    {
        ans=1ll*ans*10;
        ans%=mod;
    }
    ll s=(1<<x+y+z)-1;//s是第x+y+z位是1的要枚举的所有情况。
    ll u=(1<<x-1)|(1<<x+y-1)|(1<<x+y+z-1);//第x位,x+y位,x+y+z位是1的情况(满足的要求)
    d[0][0]=1;
    for(ll i=1;i<=n;i++)
    {
        for(ll j=0;j<=s;j++)
        {
            for(ll k=1;k<=10;k++)//添加一位
            {
                ll t=(j<<k)|(1<<k-1);//把j的然后再后面接上新添加的一位k。
                t&=s;//保持位数相同。
                if((t&u)!=u)//不满足的情况。
                {
                    d[i][t]=(d[i][t]+d[i-1][j])%mod;//d[i-1][j]是没有加k那个数的状态。
                }
            }
        }
    }
    for(ll i=0;i<=s;i++)
    {
        ans=(ans-d[n][i]+mod)%mod;
    }
    cout<<ans%mod<<endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值