HDU 6121 Build a tree【】
来源:互联网 发布:java 字符串数组赋值 编辑:程序博客网 时间:2024/06/05 23:40
Build a tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 1036 Accepted Submission(s): 416
Problem Description
HazelFan wants to build a rooted tree. The tree has n nodes labeled 0 to n−1 , and the father of the node labeled i is the node labeled ⌊i−1k⌋ . HazelFan wonders the size of every subtree, and you just need to tell him the XOR value of these answers.
Input
The first line contains a positive integer T(1≤T≤5) , denoting the number of test cases.
For each test case:
A single line contains two positive integersn,k(1≤n,k≤1018) .
For each test case:
A single line contains two positive integers
Output
For each test case:
A single line contains a nonnegative integer, denoting the answer.
A single line contains a nonnegative integer, denoting the answer.
Sample Input
25 25 3
Sample Output
76
Source
2017 Multi-University Training Contest - Team 7
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<queue>#include<stack>#include<vector>#include<map>#include<set>#include<algorithm>using namespace std;#define ll long long#define ms(a,b) memset(a,b,sizeof(a))const int M=1e5+10;const int MM=2e3+10;const int inf=0x3f3f3f3f;const int mod=1e9+7;;const double eps=1e-8;const double pi=acos(-1.0);ll n,m,k;ll tre[240]={1};ll ans,cur,last,x,l,r,full;void dfs(ll x){ ans^=x; x--; ll p=1,xx=x,ans1=0,ans2=0,pp,sum=0,g=1; while(xx>=p*k) {xx-=p*k,p*=k;} pp=p/k; while(pp) { sum+=g; if(pp&1) ans1^=sum; pp/=k; g*=k; } if(xx%p) dfs(sum+xx%p); sum=0,g=1,pp=p; while(p) { sum+=g; if(p&1) ans2^=sum; p/=k; g*=k; } ll cnt2=xx/pp; ll cnt1=k-cnt2-(xx%pp?1:0); if(cnt2&1) ans^=ans2; if(cnt1&1) ans^=ans1;}int main(){ int t; scanf("%d",&t); while(t--){ scanf("%lld%lld",&n,&k); if(k==1){ switch(n%4){ case 1:printf("1\n");break; case 2:printf("%lld\n",n+1);break; case 3:printf("0\n");break; case 4:printf("%lld\n",n);break; } continue; } ans=0; dfs(n); printf("%lld\n",ans); } return 0;}
阅读全文
0 0
- HDU 6121 Build a tree
- hdu 6121 Build a tree
- hdu 6121 Build a tree
- HDU 6121Build a tree
- hdu--6121--Build a tree
- hdu--6121:Build a tree
- hdu 6121 Build a tree
- HDU-6121 Build a tree
- hdu 6121 Build a tree
- HDU 6121 Build a tree【】
- HDU 6121 Build a tree [想法题]
- HDU 6121 Build a tree [想法题]
- hdu 6121 Build a tree (模拟)
- HDU 6121 Build a tree (技巧)
- hdu 6121 Build a tree (图论)
- HDU 6121 Build a tree(递归)
- hdu 6121(Build a tree) 瞎搞+麻烦
- HDU 6121 Build a tree(找规律+implement)
- Java集合类操作优化经验总结
- 浅谈字节序(Byte Order)及其相关操作
- HDU 6083 度度熊的午饭时光(01背包)
- 透彻理解KMP算法
- Linux系统下安装 rz、sz 命令及使用说明
- HDU 6121 Build a tree【】
- filter:过滤器
- C#+Winform 实现切换用户功能
- BZOJ 2301 [HAOI2011]Problem b
- jquery中的ajax请求用法以及参数详情
- HUD2056 Rectangles
- Android Studio 报Error:(29, 17) Failed to resolve: junit:junit:4.12的解决方法
- linux上执行cp时,总是会提示overwrite,如何不提示呢?
- iOS中关联对象的简单使用objc_setAssociatedObject