【codeforces 514E】Darth Vader and Tree
【题目链接】: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的更多相关文章
- 【30.36%】【codeforces 740D】Alyona and a tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 764C】Timofey and a tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【Codeforces 682C】Alyona and the Tree
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 设dis[v]表示v以上的点到达这个点的最大权值(肯定是它的祖先中的某个点到这个点) 类似于最大连续累加和 当往下走(x,y)这条边的时候,设 ...
- 【Codeforces 639B】Bear and Forgotten Tree 3
[链接] 我是链接,点我呀:) [题意] [题解] 首先,因为高度是h 所以肯定1下面有连续的h个点依次连成一条链.->用了h+1个点了 然后,考虑d这个约束. 会发现,形成d的这个路径,它一定 ...
- 【CodeForces - 682C】Alyona and the Tree(dfs)
Alyona and the Tree Descriptions 小灵决定节食,于是去森林里摘了些苹果.在那里,她意外地发现了一棵神奇的有根树,它的根在节点 1 上,每个节点和每条边上都有一个数字. ...
- Codeforces 514E Darth Vader and Tree 矩阵快速幂
Darth Vader and Tree 感觉是个很裸的矩阵快速幂, 搞个100 × 100 的矩阵, 直接转移就好啦. #include<bits/stdc++.h> #define L ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【19.77%】【codeforces 570D】Tree Requests
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【27.91%】【codeforces 734E】Anton and Tree
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- 全面解析布局(Grid & Canvas &StackPanel &Wrappanel) 转
写这篇文章前,特意在百度搜索了一下,发现目前网上介绍布局的文章不多,质量也不是很高.拿grid和canvas来讲,这两个布局容器还是有许多小细节值得讲的,如果你不了解的话,开发中经常会遇到一些让人匪夷 ...
- XAML实例教程系列 - 对象和属性(二)
XAML实例教程系列 - 对象和属性 2012-05-22 14:18 by jv9, 1778 阅读, 6 评论, 收藏, 编辑 在前一篇已经介绍XAML概念:“XAML语言是Extensible ...
- 关于OpenFileDialog的使用 2(转)
关于OpenFileDialog的使用 (2010-03-25 12:51:33) 标签: 杂谈 分类: WinForm 1. OpenFileDialog控件有以下基本属性 InitialDirec ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
- bzoj3545
线段树合并+离线+启发式合并 半年前这道题t成狗... 离线的做法比较好想,按照边的权值排序,询问的权值排序,然后枚举询问不断加边,加到上限后查找第k大值,这里平衡树,权值线段树都可以实现. 那么我们 ...
- ubuntu16.04 Kafka 安装
Kafka核心概念: 下面介绍Kafka相关概念,以便运行下面实例的同时,更好地理解Kafka. 1. Broker Kafka集群包含一个或多个服务器,这种服务器被称为broker 2. Topic ...
- Oracle查询列重命名
select count(*) 呼入量 from crm_cisco_call_detail
- E20170805-hm
mechanize vt. 使机械化; 用机械装置;
- Netty编解码技术和UDP实现
背景 作为网络传输框架,免不了传输对象,对象在传输之前就要序列化,这个序列化的过程就是编码过程.接收到编码后的数据就需要解码,还原传输的数据. 编解码技术就是java序列化技术,序列化的目的有两个,一 ...
- [转]linux grep命令
转自:http://www.cnblogs.com/end/archive/2012/02/21/2360965.html 1.作用Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表 ...