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 ...
随机推荐
- Linux 实用指令(10)-RPM和YUM
目录 RPM 和 YUM 1 rpm 包的管理 1.1 介绍 1.2 rpm包的简单查询指令 1.3 rpm包名基本格式 1.4 rpm包的其他查询指令: 1.5 卸载rpm 包 1.6 安装rpm包 ...
- 2019-8-16-调试时限制程序使用-CPU-核心数模拟低端设备
title author date CreateTime categories 调试时限制程序使用 CPU 核心数模拟低端设备 lindexi 2019-08-16 16:11:32 +0800 20 ...
- java排序及泛型
一.用泛型实现快排,可以传入不通类型进行排序,比如String数组,Integer数组. /** * 快速排序 * * @author chx * */ public class QuickSort ...
- grunt完整的配置demo
const path = require('path') const fs = require('fs'); module.exports = function (grunt) { grunt.reg ...
- Mysql查漏补缺
Mysql查漏补缺 存储引擎 数据库使用存储引擎来进行CRUD的操作,不同的存储引擎提供了不同的功能.Mysql支持的存储引擎有InnoDB.MyISAM.Memory.Merge.Archive.F ...
- Apache虚拟目录实现同一个IP绑定多个域名
在前:我使用的是Xampp,所以路径可能不同 找到apache\conf\extra\httpd-vhosts.conf, 如果没有的话请自己新建httpd-vhosts.conf文件, 并且在htt ...
- Python 学习杂项
#print("Hello World!") #name = "nihfjkds" age = 454 num1 = 1 num2 = 2 #print(nam ...
- elasticsearch的索引清理
curl -XDELETE 'http://127.0.0.1:9200/winlogbeat-6.0.0-2017.07.*' 脚本加api删除(推荐) cat es-index-clear.sh ...
- HTTP协议请求篇
http协议的基本概念 超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.是工作在tcp/ip协议基础上的,所有的WWW文件都必须 ...
- python 中的 用chr()数值转化为字符串,字符转化为数值ord(s)函数
1.1 python字符串定义 #!/usr/bin/python # -*- coding: utf8 -*- # 定义一个字符串 s1 = 'this is long String that sp ...