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 ...
随机推荐
- WebService接口测试
- Hive中SQL查询转换成MapReduce作业的过程
- Rsync 参数
# rsync -v, --verbose 详细模式输出 -q, --quiet 精简输出模式 -c, --checksum 打开校验开关,强制对文件传输进行校验 -a, --archive 归档模式 ...
- django静态文件的配置
Django static静态配置文件 对于Django来说静态文件一般交由Web服务器处理,Django本身不处理静态文件.为了使Django开发环境能够处理静态文件,Django有和生产环境不 ...
- 查看收到的邮件的来源ip以及包信息
有时我们需要知道收到的邮件是从哪台服务器发送过来的,或者想知道该邮件的报文头是怎样的.以下以网易邮箱为例介绍如果抓取这些信息. 首先我们需要知道网易邮箱的访问服务器(拉协议),由于SMTP是推的协议, ...
- Excel-根据分隔符将一个单元格的内容分发到多个单元格
A1 1:2:3:4:5:6:7::::10 现在想将A1根据";"进行分离,再讲分离出来的一个值填到一个单元格中 =TRIM(MID(SUBSTITUTE($A$12," ...
- day19 装饰器
Python之路,Day7 = Python基础7 randomwrapper 包装材料:包装纸:书皮global a 全局的(也就是,函数最外面的那个)nonlocal a 局部的,上层的函数的变量 ...
- 基于V8引擎的C++和JS的相互交互
基于什么原因略! 1. 脚本引擎的基本功能 V8只是一个JS引擎.去除它的特点功能出处,它必须要实现JS引擎的几个基础功能: 脚本执行:脚本可能是一个表达式:一段js代码:或者一个文件执行表达式返回j ...
- tomcat7 linux service
1. 创建tomcat用户 useradd -r -m -d /usr/local/tomcat7 -s /sbin/nologin tomcat 2. 将下面脚本命名为tomcat7 放入/etc/ ...
- php开发面试题---面试常用英语(你能介绍你自己吗?)
php开发面试题---面试常用英语(你能介绍你自己吗?) 一.总结 一句话总结: Could you please describe yourself? 1.为什么觉得自己适合这份工作? Why do ...