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 ...
随机推荐
- duboo注解使用详解
一.背景 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行. 当越来越的的接口与实现类的增加后,duboo的xml配置会越来越多,为了防止 ...
- sql 递归查询,刁刁的
with cte as( select IDPlus,SuperiorsIDPlus,RoleGrade,viplevel,NAME,WeixinId from Member where IDPlus ...
- canvas绘制线和矩形
###canvas绘制矩形 HTML中的元素canvas只支持一种原生的图形绘制:矩形.所有其他的图形的绘制都至少需要生成一条路径 1.绘制矩形 canvas提供了三种方法绘制矩形: ----> ...
- Sublime Text最舒服的主题
Theme - Afterglow (官网链接) 贴上 preferences -> settings 里面的配置 { "theme": "Afterglow-gr ...
- 线性dp——cf1012C好题
比较套路的dp题 /* dp[i][j][0|1]:前i座山盖了j座房子,第i座不盖|盖 dp[i][j][0]=min( dp[i-1][j][0] , dp[i-1][j][1]+max(0,a[ ...
- 【转载】Delphi异常处理try except语句和try finally语句用法以及区别
Delphi异常处理try except语句和try finally语句用法以及区别 一直写程序都没管他们,也尽量很少用,今天终于想把他给弄个明白,在网上找来,记下!主要是前面小部分,后面的是详细说明 ...
- hysbz3676 回文串 回文自动机
回文自动机模板题 头铁了一下午hdu6599,最后发现自己的板有问题 先放这里一个正确性得到基本确认的板,过两天肝hdu6599 #pragma GCC optimize(2) #include< ...
- python的基本数据类型与字符串的操作
一.基本数据类型 (int, bool, str ) int: 整数 str:字符串,一般不存放大量的数据 bool:布尔值,用来判断.True ,Flase list:列表,用来存放大量的数据. [ ...
- Classpath in jar关于java加载第三方jar的集中方法和详细解释。
转载地址:http://www.iteye.com/topic/332580 大家都知道一个java应用项目可以打包成一个jar,当然你必须指定一个拥有main函数的main class作为你这个ja ...
- Linux 中执行Shell 脚本的方式(三种方法)
Shell 脚本的执行方式通常有如下三种: (1)bash script-name 或者 sh script-name:(2)path/script-name或者./script-name:(3)so ...