【题目链接】: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. [POI 2007] Zap

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1101 [算法] 首先 , 问题可以转化为求GCD(x,y) = 1,x <= ...

  2. IDEA Spark Streaming Kafka数据源-Consumer

    import org.apache.spark.SparkConf import org.apache.spark.streaming.kafka.KafkaUtils import org.apac ...

  3. 62. ExtJS + fileuploadfield实现文件上传

    转自:https://www.cnblogs.com/yzuzhang/p/5128174.html 后台服务端接收文件的代码: /** * 后台上传文件处理Action */ @RequestMap ...

  4. 8.2 OSI模型

    OSI模型它是为了使不同的网络厂商.硬件厂商它们的系统能够良好的进行兼容,进行互连而提出来的,是由ISO(国际标准化组织在1979年公布的),它是现在的计算机网络领域的金科玉律.大家都认可的一个标准, ...

  5. poi导出excel改变标题颜色

    在excelutil类里面添加 public class ExcelUtil { public static Workbook fillExcelData(ResultSet rs, Workbook ...

  6. UNIX环境高级编程--7

    进程环境main函数:    C程序总是从main函数开始执行.main函数原型是:    int main(int argc, char *argv[]);    当内核执行C程序时(使用一个exe ...

  7. 浏览器被“hao123.3377.com”主页劫持的解决办法

    问题描述: 浏览器被一个叫做hao123.3377的类似hao123网址导航的家伙,强行贴上.狗皮膏药一样. 问题解决: 尝试了网上说的包括下载360什么的,都不咋好用.后来发现是在激活win10(盗 ...

  8. Java Controller下兼容xls和xlsx且可识别合并单元格的excel导入功能

    1.工具类,读取单元格数据的时候,如果当前单元格是合并单元格,会自动读取合并单元格的值 package com.shjh.core.util; import java.io.IOException; ...

  9. canvas杂记

    canvas杂记 canvas标签 <canvas width=600 height=400></canvas> //1. 拿到画布 var canvas = document ...

  10. JS——冒泡案例

    模态框 1.因为a链接和和顶级document都注册了单击事件,所以要阻止a链接向父级盒子冒泡,不然又会从document的单击事件走一遍 2.在document的单击事件中,只需要判断触发事件的目标 ...