codeforces com contest 855 problem D(数位DP)
来源:互联网 发布:软件定制开发报价 编辑:程序博客网 时间:2024/05/22 03:18
Harry, upon inquiring Helena Ravenclaw's ghost, came to know that she told Tom Riddle or You-know-who about Rowena Ravenclaw's diadem and that he stole it from her.
Harry thought that Riddle would have assumed that he was the only one to discover the Room of Requirement and thus, would have hidden it there. So Harry is trying to get inside the Room of Requirement to destroy the diadem as he knows that it is a horcrux.
But he has to answer a puzzle in order to enter the room. He is given n objects, numbered from 1 to n. Some of the objects have a parent object, that has a lesser number. Formally, objecti may have a parent object parenti such that parenti < i.
There is also a type associated with each parent relation, it can be either of type1 or type 2. Type 1 relation means that the child object is like a special case of the parent object. Type2 relation means that the second object is always a part of the first object and all its special cases.
Note that if an object b is a special case of objecta, and c is a special case of objectb, then c is considered to be a special case of objecta as well. The same holds for parts: if objectb is a part of a, and objectc is a part of b, then we say that objectc is a part of a. Also note, that if objectb is a part of a, and objectc is a special case of a, then b is a part of c as well.
An object is considered to be neither a part of itself nor a special case of itself.
Now, Harry has to answer two type of queries:
- 1 u v: he needs to tell if object v is a special case of object u.
- 2 u v: he needs to tell if object v is a part of object u.
First line of input contains the number n (1 ≤ n ≤ 105), the number of objects.
Next n lines contain two integer parenti and typei ( - 1 ≤ parenti < iparenti ≠ 0, - 1 ≤ typei ≤ 1), implying that thei-th object has the parent parenti. (If typei = 0, this implies that the objecti is a special case of object parenti. If typei = 1, this implies that the objecti is a part of object parenti). In case the i-th object has no parent, both parenti andtypei are-1.
Next line contains an integer q (1 ≤ q ≤ 105), the number of queries.
Next q lines each represent a query having three space separated integerstypei, ui, vi (1 ≤ typei ≤ 2, 1 ≤ u, v ≤ n).
Output will contain q lines, each containing the answer for the corresponding query as "YES" (affirmative) or "NO" (without quotes).
You can output each letter in any case (upper or lower).
3-1 -11 02 021 1 32 1 3
YESNO
3-1 -11 01 122 2 32 3 2
YESNO
In test case 1, as object 2 is a special case of object 1 and object 3 is a special case of object 2, this makes object3 a special case of object 1.
In test case 2, as object 2 is a special case of object 1 and object 1 has object 3, this will mean that object 2 will also have object 3. This is because when a general case (object1) has object 3, its special case (object2) will definitely have object 3.
魔法数的定义是:b进制下所有数字出现次数都是偶数次
解:一开始多开了一维状态记录 每一位是否出现偶数次 结果T到死 因为最多只有10进制 直接用异或状态是否为0 就能快速判断
还是太菜 没有关注到 进制只有10位这个有利条件
#include <bits/stdc++.h>using namespace std;typedef long long LL;int pos[100];LL dp[100][11][2048];int tmp[11];LL l, r;int b;LL dfs(int px,int pre,int limit,int k,int num){ if(px==0) { if(k==0) return 0; if(num!=0) return 0; return 1; } if(!limit&&k!=0&&dp[px][b][num]!=-1) return dp[px][b][num]; int m=limit?pos[px]:b-1; LL ans=0; for(int i=0; i<=m; i++) { if(k==0) { if(i==0) ans+=dfs(px-1,pre,limit&&i==m,0,num); else ans+=dfs(px-1,i,limit&&i==m,1,num^(1<<i)); } else ans+=dfs(px-1,i,limit&&i==m,1,num^(1<<i)); } if(!limit&&k!=0) dp[px][b][num]=ans; return ans;}LL solve(LL x,int b){ if(x==0) return 0; int k=0; while(x) { pos[++k]=x%b; x/=b; } return dfs(k,0,1,0,0);}int main(){ memset(dp,-1,sizeof(dp)); int q; scanf("%d", &q); while(q--) { scanf("%d %I64d %I64d",&b,&l,&r); printf("%I64d\n",solve(r,b)-solve(l-1,b)); } return 0;}
- codeforces com contest 855 problem D(数位DP)
- http://codeforces.com/contest/263/problem/D
- http://codeforces.com/contest/366/problem/D
- http://codeforces.com/contest/368/problem/D
- http://codeforces.com/contest/432/problem/D
- codeforces contest 855 problem C(树形DP)
- codeforces contest/796/problem/D
- 【数位DP】 【CodeForces 55D】
- codeforces 55D 数位DP
- codeforces 401D (数位DP)
- CodeForces 401D 数位DP
- Codeforces 55D 数位DP
- Codeforces 55d 数位dp
- CodeForces 55D 数位DP
- CodeForces 628D(数位DP)
- http://codeforces.com/contest/6/problem/A
- http://codeforces.com/contest/7/problem/B
- http://codeforces.com/contest/7/problem/A
- 一款能突破百度云盘限速破解浏览器V2.21.2 | 百度云盘限速破解搭配迅雷全速下载 | 百度云盘限速问题解决方案
- c中char int float double long所占字节数
- Hibernate API(常用)
- git与idea
- 基于S3C2440的嵌入式Linux驱动——AT24C02(EEPROM I2C接口)驱动解读
- codeforces com contest 855 problem D(数位DP)
- 向量点乘(内积)和叉乘(外积、向量积)概念及几何意义解读
- Several ports (8005, 8080, 8009) required by MyEclipse Tomcat v7.0 are already in use. The server ma
- (重要!)Java中的成员初始化顺序和内存分配过程static
- Android的WebView使用之GET请求
- 压缩算法核心代码Java
- 木乃伊迷宫
- java的变量类型
- 初学HTML+CSS练习搭建个人博客笔记