Problem A. Googol String Google APAC 2016 University Test Round A
2017-02-26 14:46
405 查看
将switch(reverse(S))操作记为f(S),那么f(S+0+f(S))=S+1+f(S)。任意一个字符串Sm,中间是0,左半边一定是字符串Sm-1。因此Kth
character一定是出现在某个字符串Sn里面的。
Sn构成一个二叉树,中间的0是根节点,左孩子是0,右孩子是1。然后按照左孩子->根节点->右孩子的顺序遍历可以得出Sn。
Sn的长度记为Ln,那么L0=0, Ln=2L_{n-1}+1=O(2^n)。具体的通项公式我没推出来,但是2^64>1e18,所以直接预处理循环计算n=0 to 64的Ln即可。枚举可以得出K在哪一个Ln里。
之后按照类似于二分搜索树的方法二分搜索,如果K==mid,说明搜到了节点K,再根据mid是左孩子or右孩子返回0 or 1.如果K<mid,说明应该在左子树继续搜索,然后子问题里,字符串长度就是mid-1。如果K>mid,应该在右子树继续搜索,子问题里的字符串长度是mid-1,K变为K-mid,因为要从mid+1是第一个字符,从1开始重新编号。
#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<string>
#include<cmath>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<set>
using namespace std;
//2016 Round A Problem A. Googol String
int T;
const int maxn=1010;
long long K;
int N;
long long L[100];
int bst(long long Ln,int dir)//dir=0 is left child, dir=1 is right child
{//idx should start from 1
long long mid=(Ln-1)/2+1;
if(K==mid)
{
if(dir==0)
{
return 0;
}
else if(dir==1)
{
return 1;
}
}
else if(K<mid)
{
return bst(mid-1,0);
}
else if(K>mid)
{
K-=mid;
return bst(mid-1,1);
}
}
int main()
{
freopen("A-small-practice.in","r",stdin);
// freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
scanf("%d",&T);
L[0]=0;
for(int n=1;n<64;n++)
{
L
=2*L[n-1]+1;
}
for(int ca=1;ca<=T;ca++)
{
scanf("%lld",&K);
for(int n=1;n<64;n++)
{
//if((1<<(n-1)+n-1)<K&&((1<<n+n)>=K))
if(L[n-1]<K&&L
>=K)
{
N=n;
break;
}
}
//K--;
//cout<<N<<" "<<L
<<endl;
printf("Case #%d: %d\n",ca,bst(L
,0));
}
return 0;
}
character一定是出现在某个字符串Sn里面的。
Sn构成一个二叉树,中间的0是根节点,左孩子是0,右孩子是1。然后按照左孩子->根节点->右孩子的顺序遍历可以得出Sn。
Sn的长度记为Ln,那么L0=0, Ln=2L_{n-1}+1=O(2^n)。具体的通项公式我没推出来,但是2^64>1e18,所以直接预处理循环计算n=0 to 64的Ln即可。枚举可以得出K在哪一个Ln里。
之后按照类似于二分搜索树的方法二分搜索,如果K==mid,说明搜到了节点K,再根据mid是左孩子or右孩子返回0 or 1.如果K<mid,说明应该在左子树继续搜索,然后子问题里,字符串长度就是mid-1。如果K>mid,应该在右子树继续搜索,子问题里的字符串长度是mid-1,K变为K-mid,因为要从mid+1是第一个字符,从1开始重新编号。
#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<string>
#include<cmath>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<set>
using namespace std;
//2016 Round A Problem A. Googol String
int T;
const int maxn=1010;
long long K;
int N;
long long L[100];
int bst(long long Ln,int dir)//dir=0 is left child, dir=1 is right child
{//idx should start from 1
long long mid=(Ln-1)/2+1;
if(K==mid)
{
if(dir==0)
{
return 0;
}
else if(dir==1)
{
return 1;
}
}
else if(K<mid)
{
return bst(mid-1,0);
}
else if(K>mid)
{
K-=mid;
return bst(mid-1,1);
}
}
int main()
{
freopen("A-small-practice.in","r",stdin);
// freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
scanf("%d",&T);
L[0]=0;
for(int n=1;n<64;n++)
{
L
=2*L[n-1]+1;
}
for(int ca=1;ca<=T;ca++)
{
scanf("%lld",&K);
for(int n=1;n<64;n++)
{
//if((1<<(n-1)+n-1)<K&&((1<<n+n)>=K))
if(L[n-1]<K&&L
>=K)
{
N=n;
break;
}
}
//K--;
//cout<<N<<" "<<L
<<endl;
printf("Case #%d: %d\n",ca,bst(L
,0));
}
return 0;
}
相关文章推荐
- Problem B. gCube Google APAC 2016 University Test Round A
- Round A APAC Test 2016 Problem A. Googol String
- Problem C. gCampus Google APAC 2016 University Test Round A
- Problem D. gMatrix Google APAC 2016 University Test Round C
- Problem B. gWheels Google APAC 2016 University Test Round B
- Problem B. gFiles Google APAC 2016 University Test Round C
- Problem A. Dynamic Grid Google APAC 2016 University Test Round D
- Problem C. gNumbers Google APAC 2016 University Test Round B
- Problem A. gRanks Google APAC 2016 University Test Round C
- Problem A. Travel Google APAC 2016 University Test Round B
- Problem B. Sherlock and Watson Gym Secrets Google APAC 2017 University Test Round B
- Problem C. Watson and Intervals Google APAC 2017 University Test Round B
- Problem A. Monster Path Google APAC 2017 University Test Round C
- Problem B. Beautiful Numbers Google APAC 2017 University Test Round E
- Problem B. Safe Squares Google APAC 2017 University Test Round C
- Problem C. Partioning Number Google APAC 2017 University Test Round E
- Problem C. Evaluation Google APAC 2017 University Test Round C
- Problem C. Not So Random Google APAC 2017 University Test Practice Round
- Problem A. Lazy Spelling Bee Google APAC 2017 University Test Practice Round
- Problem B. Robot Rock Band Google APAC 2017 University Test Practice Round