【题目链接】: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. 码位(code position/point)Unicode 编码与 Python 2/3 编码兼容性问题

    Unicode HOWTO 0. 码位(code position/point) 一个码位由某个数值表示,全部码位共同构成其码值空间(code space). ASCII,0~7Fhex(128) 拓 ...

  2. JSP-Runoob:JSP 国际化

    ylbtech-JSP-Runoob:JSP 国际化 1.返回顶部 1. JSP 国际化 在开始前,需要解释几个重要的概念: 国际化(i18n):表明一个页面根据访问者的语言或国家来呈现不同的翻译版本 ...

  3. JSP-Runoob:JSP 页面重定向

    ylbtech-JSP-Runoob:JSP 页面重定向 1.返回顶部 1. JSP 页面重定向 当需要将文档移动到一个新的位置时,就需要使用JSP重定向了. 最简单的重定向方式就是使用respons ...

  4. createrepo -g /enp/comps.xml .

    cd /enp; createrepo -g /enp/comps.xml .

  5. Flume Avor Source

    1.cd /usr/local2/flume/conf sudo vim avro.conf: a1.sources = r1 a1.sinks = k1 a1.channels = c1 # Des ...

  6. spark 操作Hive时遇到的问题

    To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).17/10/14 ...

  7. JavaScript--编程挑战

    小伙伴们,请编写"改变颜色"."改变宽高"."隐藏内容"."显示内容"."取消设置"的函数,点击相应 ...

  8. 题解报告:hdu 1162 Eddy's picture

    Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to become ...

  9. [转]Window2008站点安全设置,IIS7/IIS7.5中目录执行权限的设置方法

    本文转自:http://blog.snsgou.com/post-510.html 最近帮一个朋友管理Window 2008服务器,发现有个站点是用asp写的,更可怕的是还有传说中的“上传漏洞”,在上 ...

  10. 入门开发工具idea常见问题之选项中没有servlet

    1.在maven中如果创建不了servlet,在project Setting旁边的添加一个web选项,就可以创建servlet了. 初次接触这个陌生的工具还是不太好弄.