codeforces 514E-Darth Vader and Tree
题意:有个无限大的有根树,每个节点都有N个孩子,每个孩子距离父亲节点的距离为di.求距离根节点距离<=x的节点个数.
思路:注意观察数据范围,每一个d[i]均小于等于100所以我们可以设dp[i]表示距离原点i的点的个数,sum[i]表示总和,最后求sum[x]即可。
状态转移方程
for(int i=;i<=;i++)
{
for(int j=;j<=i;j++)
{
dp[i]=(dp[i]+dp[i-j]*cnt[j])%MOD;
}
sum[i]=(sum[i-]+dp[i])%MOD;
}
但是因为x太大,而且dp[n+1]是每个dp[i]*cnt[n+1-i],令N=100,保存n+1前100个数,并用第101列更新sum,用矩阵快速幂求解即可。(C是单位矩阵!!!)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD=(ll)1e9+; struct matrix
{
ll x,y;
ll a[][];
matrix(){
};
matrix(ll xx,ll yy):x(xx),y(yy)
{
memset(a,,sizeof(a));
}
}Base; matrix mul (matrix A,matrix B)
{
matrix C(A.x,B.y);
for(int i=;i<=A.x;i++)
{
for(int j=;j<=B.y;j++)
{
for(int k=;k<=B.x;k++)
{
C.a[i][j]=(C.a[i][j]+A.a[i][k]*B.a[k][j])%MOD;
}
}
}
return C;
} ll n,x,cnt[],sum[],dp[];
void init()
{
memset(cnt,,sizeof(cnt));
scanf("%lld%lld",&n,&x);
for(int i=;i<=n;i++)
{
int p;
scanf("%d",&p);
cnt[p]++;
}
memset(sum,,sizeof(sum));
dp[]=; sum[]=;
for(int i=;i<=;i++)
{
for(int j=;j<=i;j++)
{
dp[i]=(dp[i]+dp[i-j]*cnt[j])%MOD;
}
sum[i]=(sum[i-]+dp[i])%MOD;
} Base.x=; Base.y=;
//123...97 98 99 100 101(ans) //000...000 len[100] len[100]
//100...000 len[99] len[99]
//010...000 len[98] len[98]
//.........................
//000...001 len[1] len[1]
//000...000 0 1
for(int i=;i<=;i++)
{
Base.a[i+][i]=;
Base.a[i][]=cnt[-i];
Base.a[i][]=cnt[-i];
}
Base.a[][]=Base.a[][]=cnt[];
Base.a[][]=;
} matrix qpow (matrix Base,ll b)
{
matrix C(Base.x,Base.y);
for(int i=;i<=;i++) C.a[i][i]=;//danweijuzhen
while(b)
{
if(b%==) C=mul(C,Base);
b/=;
Base=mul(Base,Base);
}
return C;
} int main()
{
init();
matrix A(,);
for(int i=;i<=;i++) A.a[][i]=dp[i];
A.a[][]=sum[];
if(x<=)
{
printf("%lld\n",sum[x]);
return ;
}
A=mul(A,qpow(Base,x-));
printf("%lld\n",A.a[][]%MOD);
return ;
}
codeforces 514E-Darth Vader and Tree的更多相关文章
- Codeforces 514E Darth Vader and Tree 矩阵快速幂
Darth Vader and Tree 感觉是个很裸的矩阵快速幂, 搞个100 × 100 的矩阵, 直接转移就好啦. #include<bits/stdc++.h> #define L ...
- 【codeforces 514E】Darth Vader and Tree
[题目链接]:http://codeforces.com/problemset/problem/514/E [题意] 无限节点的树; 每个节点都有n个儿子节点; 且每个节点与其第i个节点的距离都是ai ...
- codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(启发式合并)
codeforces 741D Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...
- codeforces 812E Sagheer and Apple Tree(思维、nim博弈)
codeforces 812E Sagheer and Apple Tree 题意 一棵带点权有根树,保证所有叶子节点到根的距离同奇偶. 每次可以选择一个点,把它的点权删除x,它的某个儿子的点权增加x ...
- codeforces 220 C. Game on Tree
题目链接 codeforces 220 C. Game on Tree 题解 对于 1节点一定要选的 发现对于每个节点,被覆盖切选中其节点的概率为祖先个数分之一,也就是深度分之一 代码 #includ ...
- Codeforces E. Alyona and a tree(二分树上差分)
题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces 379 F. New Year Tree
\(>Codeforces \space 379 F. New Year Tree<\) 题目大意 : 有一棵有 \(4\) 个节点个树,有连边 \((1,2) (1,3) (1,4)\) ...
- 【27.91%】【codeforces 734E】Anton and Tree
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- codeforces 342E :Xenia and Tree
Description Xenia the programmer has a tree consisting of n nodes. We will consider the tree nodes i ...
随机推荐
- dialogs打开对话框选定文件夹,getopenfilename获取文件名
如果需要使用“打开”.“打印”等Excel内置对话框已经具有的功能,可以使用代码直接调用这些内置的对话框,如下面的代码所示. #001 Sub DialogOpen() #002 Appl ...
- Delphi 第2课
项目文件.dpr单元文件.pas--目标文件.dcu窗体文件.dfu begin 对象名.属性 对象名.方法end; 在delphi 自动提示控制语句结构 键盘输入Ctrl 就可以看到了 代码错误:( ...
- sql (6) exists
SQL中EXISTS的用法 查询所有选修了1号课程的学生的姓名,年龄,性别首先取Student表中的一个元组,然后在SC表中依次找SC.Sno=该元组的So,并且对应的Cno='2',如果存在,则外层 ...
- java6大原则之单一职责原则,里式替换原则
单一职责原则:一个接口,一个类,一个方法,最好只做一类事,当然,在真实的项目中,一系列因素下,很难做到单一职责原则,但是针对接口是可以做到的,方法和类要尽量做到 里式替换原则:父类出现的地方,换成子类 ...
- 【JZOJ6388】小w的作业
description analysis 二分一个角度,首先假设该弧度角\(\theta \in[{\pi \over 2},\pi]\),要找的直线斜率\(k\in(-∞,\tan\theta]\) ...
- 【JZOJ6367】工厂(factory)
description 大神 wyp 开了家工厂,工厂有 n 个工人和 p 条流水线. 工厂的工人都是睡神,因此第 i 个工人只会在 si 至 ti 时刻才会工作. 每个工人都会被分派到一条流水线上, ...
- 校园商铺-2项目设计和框架搭建-9验证Service
1. 新建接口 main: com.csj2018.o2o.service/AreaService.java package com.csj2018.o2o.service; import java. ...
- 专访阿里云MVP王俊杰:开发者的超能力是用技术让世界更美好
[王俊杰:阿里云MVP,陕西创博网络科技有限公司总经理.大数据与物联网的爱好者与实践者. 8年以上互联网从业经验,曾从事军工相关仿真分析软件研发与集成.4年以上大数据系统开发经验.目前正与天水市秦州区 ...
- day21-time与random等常用模块与包
2018-08-05 # ********************day21-time与random等常用模块与包 ******************* # 参考资料# python模块(转自Yua ...
- service sshd start启动失败,Badly formatted port number.
在做xhell学习的时候,把端口号修改了,后面忘记修改回 来,导致 [root@MyRoth 桌面]# service sshd start 正在启动 sshd:/etc/ssh/sshd_confi ...