【题目链接】:http://codeforces.com/problemset/problem/514/E

【题意】



无限节点的树;

每个节点都有n个儿子节点;

且每个节点与其第i个节点的距离都是ai;

问你与根节点的距离不超过x的节点个数;

【题解】



考虑一个非常不靠谱的DP方程

f[i]=∑(f[i-j]*cnt[j]);

这里f[i]表示与根节点的距离为i的节点个数;

cnt[j]表示ai的值中为j的ai的个数;(即与儿子节点距离为j的边的个数);

因为ai最大值为100,所以j∈[1..100]

i∈[0..x]

求和就是答案了;

但x有1e9的规模;

需要优化;

矩阵!

这里先把f[1..100]的值算出来,同时把f[1..100]累加起来->sum;

得到矩阵A



然后再构造一个系数矩阵B



cnt的意义如上;

这里之所以把sum加进去,是为了便于最后直接输出结果;

不然我们这样递推如果只得出f[x]的话,你没办法加起来;

前100列用于递推出f[i+1];

第100列用于求和∑f[1..i];



A×Bx−100

最后答案直接输出右下角那个值a[101][101]就好;

sum一开始加上一个1;

因为本身也算.



【Number Of WA】



0



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) cin >> x
#define ms(x,y) memset(x,y,sizeof x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 110;
const LL MOD = 1e9+7; int n,x;
LL dp[N],cnt[N],sum; const int G = 101; //矩阵大小
struct MX
{
int v[G+5][G+5];
void O() { ms(v, 0); }
void E() { ms(v, 0); for (int i = 1; i <= G; ++i)v[i][i] = 1; }
void P()
{
for (int i = 1; i <= G; ++i)
{
for (int j = 1; j <= G; ++j)printf("%d ", v[i][j]); puts("");
}
}
MX operator * (const MX &b) const
{
MX c; c.O();
for (int k = 1; k <= G; ++k)
{
for (int i = 1; i <= G; ++i) if (v[i][k])
{
for (int j = 1; j <= G; ++j)
{
c.v[i][j] = (c.v[i][j] + (LL)v[i][k] * b.v[k][j]) % MOD;
}
}
}
return c;
}
MX operator + (const MX &b) const
{
MX c; c.O();
for (int i = 1; i <= G; ++i)
{
for (int j = 1; j <= G; ++j)
{
c.v[i][j] = (v[i][j] + b.v[i][j]) % MOD;
}
}
return c;
}
MX operator ^ (LL p) const
{
MX y; y.E();
MX x; memcpy(x.v, v, sizeof(v));
int num[64+2],cnt = 0;
while (p)
{
num[++cnt] = p&1;
p>>=1;
}
for (int i =cnt;i>=1;i--)
{
y = y*y;
if (num[i])
y = y*x;
}
return y;
}
}a,xishu; int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false);
rei(n),rei(x);
rep1(i,1,n)
{
int d;
rei(d);
cnt[d]++;
}
dp[0] = 1;
rep1(i,1,min(x,100))
rep1(j,1,i)
dp[i] = (dp[i]+(dp[i-j]*cnt[j])%MOD)%MOD;
sum=1;
rep1(i,1,min(x,100))
sum=(dp[i]+sum)%MOD;
if (x<=100)
return cout << sum << endl,0;
a.O();
rep1(i,1,100)
a.v[1][i] = dp[i];
a.v[1][101]= sum;
xishu.O();
rep1(i,2,100)
xishu.v[i][i-1] = 1;
rep1(i,1,100)
xishu.v[i][100]=xishu.v[i][101] = cnt[101-i];
xishu.v[101][101] = 1;
a = a*(xishu^(x-100));
cout << a.v[1][101]<<endl;
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}

【codeforces 514E】Darth Vader and Tree的更多相关文章

  1. 【30.36%】【codeforces 740D】Alyona and a tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 【codeforces 764C】Timofey and a tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【Codeforces 682C】Alyona and the Tree

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 设dis[v]表示v以上的点到达这个点的最大权值(肯定是它的祖先中的某个点到这个点) 类似于最大连续累加和 当往下走(x,y)这条边的时候,设 ...

  4. 【Codeforces 639B】Bear and Forgotten Tree 3

    [链接] 我是链接,点我呀:) [题意] [题解] 首先,因为高度是h 所以肯定1下面有连续的h个点依次连成一条链.->用了h+1个点了 然后,考虑d这个约束. 会发现,形成d的这个路径,它一定 ...

  5. 【CodeForces - 682C】Alyona and the Tree(dfs)

    Alyona and the Tree Descriptions 小灵决定节食,于是去森林里摘了些苹果.在那里,她意外地发现了一棵神奇的有根树,它的根在节点 1 上,每个节点和每条边上都有一个数字. ...

  6. Codeforces 514E Darth Vader and Tree 矩阵快速幂

    Darth Vader and Tree 感觉是个很裸的矩阵快速幂, 搞个100 × 100 的矩阵, 直接转移就好啦. #include<bits/stdc++.h> #define L ...

  7. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  8. 【19.77%】【codeforces 570D】Tree Requests

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 【27.91%】【codeforces 734E】Anton and Tree

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

随机推荐

  1. tiny4412学习(三)之移植linux-4.x驱动(1)支持网卡驱动【转】

    本文转载自:http://blog.csdn.net/fengyuwuzu0519/article/details/74160686 一.思路 上一节我们通过DNW将内核.文件系统.设备树文件烧入到内 ...

  2. 选择排序(2)——堆排序(heap sort)

    前期概念: 二叉树 完全二叉树 左序遍历 中序遍历 右序遍历 堆 小根堆 大根堆 堆排序(Heapsort)是指利用堆积树(堆)这种数据结构所设计的一种排序算法,它是选择排序的一种.可以利用数组的特点 ...

  3. PCB genesis连孔加除毛刺孔(圆孔与槽孔)实现方法(二)

    一.为什么 连孔加除毛刺孔 原因是 PCB板材中含有玻璃纤维, 毛刺产生位置在于2个孔相交位置,由于此处钻刀受力不均导致纤维切削不断形成毛刺 ,为了解决这个问题:在钻完2个连孔后,在相交处再钻一个孔, ...

  4. PCB MongoDB 索引

    在索引在数据库中非常重要,当然在MongoDB也是一样啦. 一.获取索引 db.ppeflow.getIndexes() 初始化,每个集都默认_id字段为主键objectid,索引名为_id_ 二.创 ...

  5. php验证手机号是否合法

    用正则匹配手机号码的时候, 我们先分析一下手机号码的规律: 1. 手机号通常是11位的 2. 经常是1开头 3. 第二个数字通常是34578这几个数字, 2014.5.5日170号段的手机号开卖所以这 ...

  6. P3379最近公共祖先(LCA)

    题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询问的个数和树根结点的序号. 接下来N-1行每 ...

  7. codevs3327选择数字(单调队列优化)

    3327 选择数字  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond     题目描述 Description 给定一行n个非负整数a[1]..a[n].现 ...

  8. HTML--使用下拉列表框进行多选

    下拉列表也可以进行多选操作,在<select>标签中设置multiple="multiple"属性,就可以实现多选功能,在 widows 操作系统下,进行多选时按下Ct ...

  9. Jenkins-SVN + Maven + Docker

    第1步:安装插件 Subversion Plug-inMaven Integration pluginCloudBees Docker Build and Publish pluginDeploy t ...

  10. jQuery与js的区别,并有基本语法详解,

    通过过一下对比,我们能很清楚的发现jquery与js的区别,运用jquery能大量减少代码量,不过js里面关于时间的setinterval和settimeout只能用js <script src ...