C

构造一个矩阵,然后采用矩阵快速幂

#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <vector>
using namespace std;
typedef long long LL;
const LL mod = ;
const int maxn=<<;
struct Matix
{
LL m[maxn][maxn];
int n;
Matix mul(Matix &rhs)
{
Matix ans;
for(int i=; i<n; i++)
for(int j=; j<n; j++)
{
ans.m[i][j]=;
for(int k=; k<n; k++)
ans.m[i][j]=(ans.m[i][j]+m[i][k]*rhs.m[k][j])%mod;
}
ans.n=n;
return ans;
}
};
Matix T;
LL powmod(int n,int m)
{
Matix ans,A=T;
ans.n=<<m;
memset(ans.m,,sizeof(ans.m));
for(int i=; i<ans.n; i++)
ans.m[i][i]=;
while(n){
if(n&)ans=ans.mul(A);
n>>=;
A=A.mul(A);
}
LL an=;
for(int i=; i<ans.n; i++){
an=(ans.m[i][]+an)%mod;
}
return an;
}
int jud(int A, int B, int m)
{
for(int i=; i<m; i++)
{
if(A&(<<i)){
if(B&(<<i))continue;
if(i&&(B&(<<(i-)))> && (A&(<<(i-)))== )return ;
if(i!=m-&&(B&(<<(i+)))>&&(A&(<<(i+)))==)return ;
}
}
return ;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==)
{ for(int i=; i<(<<m); i++)
{
for(int j=; j<(<<m); j++)
{ T.m[i][j]=jud(i,j,m);
} }
T.n=(<<m);
printf("%I64d\n",powmod(n,m));
}
return ;
}

D

计算[a,b]之间的所有数的各个位置异或然后整个区间求和, 我们用d[i][j]表示在第i位时异或为j的个数,然后我们枚举每一位,求和就好了,因为10以内的数异或小于16

#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
using namespace std;
typedef long long LL;
const LL mod = ;
const int maxn=+;
LL dp[maxn][],d[maxn][];
void solve()
{
d[][]=;
for(int i=; i<; i++)
{
for(int j=; j<; j++)
for(int r=; r<; r++){
d[i][r]=d[i][r]+d[i-][r^j];
if(d[i][r]>=mod)d[i][r]-=mod;
}
}
}
char str[maxn];
LL look(int n,int op)
{
int nt=;
LL ans=;
for(int i=n; i>; i--){
int lit=str[n-i]-'';
for(int j=; j<lit; j++){
int dr=nt^j;
for(int k=; k<; k++){
ans=(ans+d[i-][k^dr]*k )%mod;
}
}
nt^=lit;
}
if(op)
ans=(ans+nt)%mod;
return ans;
}
int main()
{
int cas;
solve();
scanf("%d",&cas);
for(int cc= ;cc<=cas; cc++)
{
LL ans=;
scanf("%s",str);
int len=strlen(str);
ans=ans-look(len,);
scanf("%s",str);
len=strlen(str);
LL a1=look(len,);
ans=( (ans+a1)%mod+mod )%mod;
printf("Case #%d: %I64d\n",cc,ans);
}
return ;
}

E

给了一棵树50000个节点求从100000 次查询 每次给了两个点求两个节的路径最小并且最大值最大,每个叶子节点能回到1这个节点,我们考虑几种情况,

树上直接链的 , 还有就是通过叶子节点回到1 1再转化,  这里有个坑点就是 一个点可以到叶子节点后 还可能通过他的父亲到达叶子节点,不一定是通过他的孩子,坑了好久

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string.h>
#include <vector>
using namespace std;
typedef long long LL;
const int numofedg=+;
const int maxn=;
int H[maxn],nx[numofedg],to[numofedg],numofE,val[maxn];
int son[maxn],num[maxn],fa[maxn],top[maxn],p[maxn],fp[maxn],pos,depth[maxn],ans[maxn];
int minDown[maxn],maxDown[maxn],minUp[maxn],maxUp[maxn];
void init(int n)
{
val[]=;maxUp[]=minUp[]=;
numofE=;pos=;
memset(H,,sizeof(H));
}
void addedg(int a, int b){
numofE++;
to[numofE]=b;
nx[numofE]=H[a];
H[a]=numofE;
}
void panduan(int &L, int &V, int L2,int V2){
if(L>=L2){
if(L == L2 ){
V=V>V2?V:V2;
}else {
L=L2;
V=V2;
}
}
}
void dfs(int cur, int per, int dep)
{
depth[cur]=dep;
son[cur]=-;
fa[cur]=per;
num[cur]=;
maxUp[cur]=max( val[ cur ] , maxUp[ per ] );
minUp[cur]=minUp[per] + val[cur];
maxDown[ cur ]=-;
minDown[ cur ]=;
for(int i=H[cur]; i> ; i=nx[i])
{
int tto=to[i];
if(tto==per)continue;
dfs(tto,cur,dep+);
num[cur]+=num[tto];
panduan(minDown[cur],maxDown[cur],minDown[tto],maxDown[tto]);
if( son[cur]==- || num[ son[cur] ] < num[ tto ] )son[cur]=tto;
}
if(son[cur]!=-){
maxDown[cur]=max(maxDown[cur],val[cur]);
minDown[cur]+=val[cur]; }else{
maxDown[cur]=val[cur];
minDown[cur]=val[cur];
}
}
void finde(int cur ,int per, int xx)
{
top[cur]=xx;
pos++;
p[cur]=pos;
fp[pos]=cur;
if(son[cur]!=-)
finde(son[cur],cur,xx);
for(int i=H[cur]; i>; i=nx[i])
{
if(to[i]==son[cur]||to[i]==per)continue;
finde(to[i],cur,to[i]);
}
}
struct Itree
{
int sum[maxn*],maxval[ maxn*],CL,CR;
int S,Ma;
void build(int L, int R, int o)
{
if(L==R)
{
maxval[ o ] = sum[ o ] = val[ fp[L] ];
return ;
}
int mid=(L+R)>>;
build(L,mid,o*);
build(mid+,R,o*+);
maxval[o]=max(maxval[o*],maxval[o*+]);
sum[o]=sum[o*]+sum[o*+];
}
void query(int L, int R, int o)
{
if(CL<=L&&R<=CR){
S=S+sum[o];
Ma=max(maxval[o],Ma); return ;
}
int mid=(L+R)>>;
if(CL<=mid)
query(L,mid,o*);
if(CR>mid)
query(mid+,R,o*+);
}
}T;
struct QE{
int L,R;
QE(int cL=,int cR=){
L=cL; R=cR;
}
};
vector<QE>Qnu;
void solve(int x, int y)
{
int f1=top[x],f2=top[y];
while(f1!=f2){
if(depth[f1]<depth[f2]){
int temp=x;x=y; y=temp;
temp=f1;f1=f2;f2=temp;
}
Qnu.push_back( QE(p[f1],p[x]) );
x=fa[f1];
f1=top[x];
}
if(depth[x]>depth[y]){
int temp=x; x=y; y=temp;
}
Qnu.push_back(QE(p[x],p[y]));
}
void jud2(int u, int v, int &L, int &V,int n)
{
Qnu.clear();
solve(u,v);
T.S=;
T.Ma=-;
for(int i=; i<Qnu.size(); i++)
{
QE t=Qnu[i];
T.CL=min(t.L,t.R);
T.CR=max(t.L,t.R);
T.query(,n,);
}
L=T.S;V=T.Ma;
}
void dfs2(int cur, int per)
{
if(cur!=){
panduan(minDown[cur],maxDown[cur],minDown[per]+val[cur],max(maxDown[per],val[cur]));
}
for(int i=H[cur]; i>; i=nx[i])
dfs2(to[i],cur);
}
void jud1(int uu, int vv, int &L, int &V)
{
L=minDown[uu]+minUp[vv];
V=max(maxDown[uu],maxUp[vv]);
int L2,V2;
L2=minDown[uu]+minDown[vv]+val[];
V2=max(maxDown[uu],max(maxDown[vv],val[]));
panduan(L,V,L2,V2); L2=minUp[uu]+minDown[vv];
V2=max(maxUp[uu],maxDown[vv]); panduan(L,V,L2,V2);
}
int main()
{
int cas,n,m;
scanf("%d",&cas);
for(int cc=; cc<=cas ;cc++)
{
scanf("%d%d",&n,&m);
init(n);
for(int i=; i<=n; i++)
{
int d;
scanf("%d",&d);
addedg(d,i);
}
for(int i=; i<=n; i++)
{
scanf("%d",&val[i]);
}
dfs(,,);
finde(,,);
T.build(,n,);
dfs2(,);
for(int i=; i<m; i++)
{
int u,v;
scanf("%d%d",&u,&v);
if(u==v){
printf("%d %d\n",val[u],val[v]);
continue;
}
int L,V;
jud1(u,v,L,V);
int L2,V2;
jud2(u,v,L2,V2,n);
panduan(L,V,L2,V2);
printf("%d %d\n",L,V);
}
} return ;
}

BestCoder Round #55 ($)的更多相关文章

  1. BestCoder Round #89 02单调队列优化dp

    1.BestCoder Round #89 2.总结:4个题,只能做A.B,全都靠hack上分.. 01  HDU 5944   水 1.题意:一个字符串,求有多少组字符y,r,x的下标能组成等比数列 ...

  2. BestCoder Round #90 //div all 大混战 一题滚粗 阶梯博弈,树状数组,高斯消元

    BestCoder Round #90 本次至少暴露出三个知识点爆炸.... A. zz题 按题意copy  Init函数 然后统计就ok B. 博弈 题  不懂  推了半天的SG.....  结果这 ...

  3. bestcoder Round #7 前三题题解

    BestCoder Round #7 Start Time : 2014-08-31 19:00:00    End Time : 2014-08-31 21:00:00Contest Type : ...

  4. Bestcoder round #65 && hdu 5593 ZYB's Tree 树形dp

    Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  5. Bestcoder round #65 && hdu 5592 ZYB's Premutation 线段树

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  6. 暴力+降复杂度 BestCoder Round #39 1002 Mutiple

    题目传送门 /* 设一个b[]来保存每一个a[]的质因数的id,从后往前每一次更新质因数的id, 若没有,默认加0,nlogn复杂度: 我用暴力竟然水过去了:) */ #include <cst ...

  7. 贪心 BestCoder Round #39 1001 Delete

    题目传送门 /* 贪心水题:找出出现次数>1的次数和res,如果要减去的比res小,那么总的不同的数字tot不会少: 否则再在tot里减去多余的即为答案 用set容器也可以做,思路一样 */ # ...

  8. BestCoder Round #88

    传送门:BestCoder Round #88 分析: A题统计字符串中连续字串全为q的个数,预处理以下或加个cnt就好了: 代码: #include <cstdio> #include ...

  9. BestCoder Round #69 (div.2) Baby Ming and Weight lifting(hdu 5610)

    Baby Ming and Weight lifting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

随机推荐

  1. python递归,装饰器,函数, 高阶函数

    在函数内部,可以调用其它函数,如果一个函数在内部调用自身本身,这个函数就是递归函数 递归特性:1.必须有一个明确的结束条件 2.每次进入更深一层递归时,问题规模比上次递归都有所减少(10-8-5等) ...

  2. MyBatis的核心组件

    MyBatis的核心组件主要分为4个部分 SqlSessionFactoryBuilder(构造器):它会根据配置或者代码来生成SqlSessionFactory,采用的是分步构建的Builder模式 ...

  3. 【SQL】如何使用SQL like 方法和SQL [charlist] 通配符(SQL like的拓展)

    1.like 相关用法 '%a'    //以a结尾的数据 'a%'    //以a开头的数据 '%a%'    //含有a的数据 ‘_a_’    //三位且中间字母是a的 '_a'    //两位 ...

  4. OC侧滑删除

    做侧滑的时候发现一个问题,当一个UITableView的cell有的有侧滑,有的没有,当用editActionsForRowAtIndexPath方法的时候发现有点问题,查看了下api,需要用到can ...

  5. 循环打印视图(学习WHILE循环)

    ) --视图名 --总视图数 --循环次数 SELECT @RowCount = COUNT(NAME) FROM sysobjects WHERE xtype = 'v' WHILE @i < ...

  6. 005-docker-镜像使用、拉取、运行、创建、打tag

    当运行容器时,使用的镜像如果在本地中不存在,docker 就会自动从 docker 镜像仓库中下载,默认是从 Docker Hub 公共镜像源下载. 1.列出所有本地镜像 docker images ...

  7. Redis入门到高可用(九)——有序集合zset

    一.数据结构 集合与有序集合,列表与有序集合的对比 二.主要API zadd 将一个或多个 member 元素及其 score 值加入到有序集 key 当中. zrem 移除有序集 key 中的一个或 ...

  8. vue 动态绑定背景图片

    html <div class="racetm" :style="{backgroundImage: 'url(' + (coverImgUrl ? coverIm ...

  9. abap 通过importing 和 exporting 调用其它函数

    1:其它函数的(输入或输出)参数名都在=号左边.

  10. vbox 相关

    1.虚拟机vbox 安装mac os 10.12 图文教程: https://www.cnblogs.com/liming2017/p/7566953.html