题意:有个无限大的有根树,每个节点都有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的更多相关文章

  1. Codeforces 514E Darth Vader and Tree 矩阵快速幂

    Darth Vader and Tree 感觉是个很裸的矩阵快速幂, 搞个100 × 100 的矩阵, 直接转移就好啦. #include<bits/stdc++.h> #define L ...

  2. 【codeforces 514E】Darth Vader and Tree

    [题目链接]:http://codeforces.com/problemset/problem/514/E [题意] 无限节点的树; 每个节点都有n个儿子节点; 且每个节点与其第i个节点的距离都是ai ...

  3. 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 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...

  4. codeforces 812E Sagheer and Apple Tree(思维、nim博弈)

    codeforces 812E Sagheer and Apple Tree 题意 一棵带点权有根树,保证所有叶子节点到根的距离同奇偶. 每次可以选择一个点,把它的点权删除x,它的某个儿子的点权增加x ...

  5. codeforces 220 C. Game on Tree

    题目链接 codeforces 220 C. Game on Tree 题解 对于 1节点一定要选的 发现对于每个节点,被覆盖切选中其节点的概率为祖先个数分之一,也就是深度分之一 代码 #includ ...

  6. Codeforces E. Alyona and a tree(二分树上差分)

    题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  7. Codeforces 379 F. New Year Tree

    \(>Codeforces \space 379 F. New Year Tree<\) 题目大意 : 有一棵有 \(4\) 个节点个树,有连边 \((1,2) (1,3) (1,4)\) ...

  8. 【27.91%】【codeforces 734E】Anton and Tree

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. codeforces 342E :Xenia and Tree

    Description Xenia the programmer has a tree consisting of n nodes. We will consider the tree nodes i ...

随机推荐

  1. linux mysql主从复制配置

    1.设置主库master的servie-id值并且开启bin-log功能参数vi /etc/my.cnf修改my.cnf的参数:[mysqld]server-id=1 //每一个库的server-id ...

  2. Vagrant安装步骤

    Vagrant安装步骤 下载添加box镜像 vagrant box add base 远端的box地址或者本地的box文件名 建立box镜像关联 vagrant box add centos72 va ...

  3. NIO教程笔记

    NIO操作文件部分详解 NIO——New IO,也可以理解为非阻塞IO(Non Blocking IO).可以替代旧IO,更高效的支持读写(文件读写,网络读写).但文件操作都是阻塞的.学习NIO首先要 ...

  4. 人脸识别-常用的数据库Face Databases From Other Research Groups

    Static/Videos Static Single/Multiple faces Single? Gray/Color Color Resolution Vaious Face pose Fron ...

  5. 数据库MySQL--分组查询

    事例使用文件:https://files.cnblogs.com/files/Vera-y/myemployees.zip 分组数据:group by 子句 分组查询语法: select 分组函数,列 ...

  6. tomcat的webapps下面包含五个自带的项目

    1.docs tomcat的介绍和操作文档等 2.examples 小程序示例 3.host-manager host管理 4.manager(重点) 进行 Server Status 和 Appli ...

  7. promise 获取文件内容

    文件结构图 { "next":"b.json", "msg":"this is a" } a.json { " ...

  8. openwrt_ipsec_function.sh 分析

    #!/bin/sh # # Copyright (C) Vitaly Protsko <villy@sft.ru> errno= # get_fieldval gate src " ...

  9. 一个切图仔的HTML笔记

    1,href="javascript:history.back(-1)" //页面返回上一步 2,meta信息设置 360浏览器就会在读取到这个标签后,立即切换对应的极速核. &l ...

  10. DDOS 单例

    DDOS.H #pragma once //g++ ../../../Main.cpp ../../../DDOS.cpp -lpthread #include <stdio.h> #in ...