codeforces514E

Darth Vader and Tree

 CodeForces - 514E 

When Darth Vader gets bored, he sits down on the sofa, closes his eyes and thinks of an infinite rooted tree where each node has exactly n sons, at that for each node, the distance between it an its i-th left child equals to di. The Sith Lord loves counting the number of nodes in the tree that are at a distance at most x from the root. The distance is the sum of the lengths of edges on the path between nodes.

But he has got used to this activity and even grew bored of it. 'Why does he do that, then?' — you may ask. It's just that he feels superior knowing that only he can solve this problem.

Do you want to challenge Darth Vader himself? Count the required number of nodes. As the answer can be rather large, find it modulo 109 + 7.

Input

The first line contains two space-separated integers n and x (1 ≤ n ≤ 105, 0 ≤ x ≤ 109) — the number of children of each node and the distance from the root within the range of which you need to count the nodes.

The next line contains n space-separated integers di (1 ≤ di ≤ 100) — the length of the edge that connects each node with its i-th child.

Output

Print a single number — the number of vertexes in the tree at distance from the root equal to at most x.

Examples

Input
3 3
1 2 3
Output
8

Note

Pictures to the sample (the yellow color marks the nodes the distance to which is at most three)

给出一个每个节点有n个孩子的多叉树,父亲到第i个孩子有固定的长度,问到根节点的距离不超过x的节点的数目。
1 <= n <= 1e5; 0 <= x <= 1e9; 1 <= di <= 100 Mod=1e9+7

sol:有一个较显然的dp,dp[i]表示深度为i的点的个数,这样就有了一个很裸的暴力

/*
给出一个每个节点有n个孩子的多叉树,父亲到第i个孩子有固定的长度,问到根节点的距离不超过x的节点的数目。
1 <= n <= 1e5; 0 <= x <= 1e9; 1 <= di <= 100  Mod=1e9+7
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
    ll s=0; bool f=0; char ch=' ';
    while(!isdigit(ch))    {f|=(ch=='-'); ch=getchar();}
    while(isdigit(ch)) {s=(s<<3)+(s<<1)+(ch^48); ch=getchar();}
    return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
    if(x<0) {putchar('-'); x=-x;}
    if(x<10) {putchar(x+'0'); return;}
    write(x/10); putchar((x%10)+'0');
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const ll N=100005,Mod=1000000007;
ll n,m,d[N];
ll dp[N],f[105];
inline void Ad(ll &x,ll y)
{
    x+=y; x-=(x>=Mod)?Mod:0;
}
int main()
{
    freopen("codeforces514E_data.in","r",stdin);
    int i,j,k;
    R(n); R(m);
    for(i=1;i<=n;i++) f[d[i]=read()]++; sort(d+1,d+n+1); n=unique(d+1,d+n+1)-d-1;
    dp[0]=1;
    for(i=1;i<=m;i++)
    {
        for(j=1;j<=min(i,100);j++) Ad(dp[i],dp[i-j]*f[j]%Mod);
    }
    ll ans=0;
    for(i=0;i<=m;i++) Ad(ans,dp[i]);
    Wl(ans);
    return 0;
}
View Code

然后发现di很小,转移方程可以用矩阵快速幂优化,发现转移dp[x]时有用的就是dp[x-100]~dp[x],而在转移dp[x+1]时有用的就是dp[x-99]~dp[x+1]了,而转移就是向暴力写的dp一样,每次只要转移最后一位就是了,前面的都可以搬过来,所以矩阵就可以推了

[0 0 0 ... 0 0 f[100] f[100]]
[1 0 0 ... 0 0 f[99] f[99]    ]
[0 1 0 ... 0 0 f[98] f[98]    ]
[0 0 1 ... 0 0 f[97] f[97]    ]
...
[0 0 0 ... 0 1 f[1] f[1]        ]
[0 0 0 ... 0 0 0 1              ]

/*
给出一个每个节点有n个孩子的多叉树,父亲到第i个孩子有固定的长度,问到根节点的距离不超过x的节点的数目。
1 <= n <= 1e5; 0 <= x <= 1e9; 1 <= di <= 100  Mod=1e9+7
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
    ll s=0; bool f=0; char ch=' ';
    while(!isdigit(ch))    {f|=(ch=='-'); ch=getchar();}
    while(isdigit(ch)) {s=(s<<3)+(s<<1)+(ch^48); ch=getchar();}
    return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
    if(x<0) {putchar('-'); x=-x;}
    if(x<10) {putchar(x+'0'); return;}
    write(x/10); putchar((x%10)+'0');
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const ll N=105,Mod=1000000007;
ll n,m,dp[N],f[N],Qzh[N];
ll a[N][N],b[N][N],c[N][N],ans[N][N];
inline void Ad(ll &x,ll y)
{
    x+=y; x-=(x>=Mod)?Mod:0;
}
int main()
{
    freopen("codeforces514E_data.in","r",stdin);
    freopen("my.out","w",stdout);
    int i,j,k;
    R(n); R(m);
    for(i=1;i<=n;i++) f[read()]++;
    dp[0]=Qzh[0]=1;
    for(i=1;i<=100;i++)
    {
        for(j=1;j<=i;j++) Ad(dp[i],dp[i-j]*f[j]%Mod);
        Qzh[i]=Qzh[i-1]; Ad(Qzh[i],dp[i]);
    }
    if(m<=100)
    {
        Wl(Qzh[m]); return 0;
    }
//    for(i=0;i<=100;i++) W(dp[i]); puts("");
    for(i=1;i<=100;i++) ans[1][i]=dp[i]; ans[1][101]=Qzh[100];
    for(i=1;i<=101;i++) a[i][i]=1;
    for(i=2;i<=100;i++) b[i][i-1]=1; b[101][101]=1;
    for(i=1;i<=100;i++) b[i][100]=b[i][101]=f[101-i];
//    for(i=1;i<=101;i++,puts("")) for(j=1;j<=101;j++) W(b[i][j]);
//    memmove(b,a,sizeof b);
    int oo=m-100;
    while(oo)
    {
        if(oo&1)
        {
            memset(c,0,sizeof c);
            for(i=1;i<=101;i++) for(j=1;j<=101;j++) for(k=1;k<=101;k++)
            {
                Ad(c[i][j],1ll*a[i][k]*b[k][j]%Mod);
            }
            memmove(a,c,sizeof a);
        }
        oo>>=1;
        memset(c,0,sizeof c);
        for(i=1;i<=101;i++) for(j=1;j<=101;j++) for(k=1;k<=101;k++)
        {
            Ad(c[i][j],1ll*b[i][k]*b[k][j]%Mod);
        }
        memmove(b,c,sizeof b);
    }
    memset(c,0,sizeof c);
    for(i=1;i<=1;i++) for(j=1;j<=101;j++) for(k=1;k<=101;k++)
    {
        Ad(c[i][j],ans[i][k]*a[k][j]%Mod);
    }
    memmove(ans,c,sizeof ans);
    Wl(ans[1][101]);
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/gaojunonly1/p/11310849.html
514