gym 101194 B Hemi Palindrome

题意

定义一个01串若其奇数位为回文或者其偶数位为回文则是个好串,问长度为n字典序第K小的好串是什么。

题解

比较套路的暴力找第k大问题,每次从高位枚举是0还是1,之后统计填0时候方案可行的方案数(使用容斥)若方案数<k说明该为是1则将k减去求的的然后,否则该位是0继续枚举。

代码

/**
 *     author:     TelmaZzzz
 *     create:     2019-10-05-11.07.16
**/
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <ctime>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
//#include <random>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
void _R(int &x) { scanf("%d", &x); }
void _R(ll &x) { scanf("%lld", &x); }
void _R(db &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
void _W(const int &x) { printf("%d", x); }
void _W(const ll &x) { printf("%lld", x); }
void _W(const db &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#define rep(x,y,z) for(int x=y;x<=z;x++)
#define erp(x,y,z) for(int x=y;x>=z;x--)
#define PB push_back
#define MP make_pair
#define INF 1073741824
#define inf 1152921504606846976
#define pi 3.14159265358979323846
#define Fi first
#define Se second
//#pragma comment(linker,"/STACK:10240000,10240000")
//mt19937 rand_(time(0));
const int N=3e5+7,M=2e6;
const long long mod=1e9+7;
inline int read(){int ret=0;char ch=getchar();bool f=1;for(;!isdigit(ch);ch=getchar()) f^=!(ch^'-');for(;isdigit(ch);ch=getchar()) ret=(ret<<1)+(ret<<3)+ch-48;return f?ret:-ret;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}//逆元
//int head[N],NEXT[M],ver[M],tot;void link(int u,int v){ver[++tot]=v;NEXT[tot]=head[u];head[u]=tot;}
void TelmaZzzz(){
#ifndef ONLINE_JUDGE
    freopen("1.txt","r",stdin);
#endif
}
 
int n;
int pos_odd;
int pos_even;
int pos_odd_last;
int pos_even_last;
int ans[N];
bool flag1,flag0;
ll Pow(int x){
    x=min(x,61);
    ll res=1;
    ll p=2;
    while(x){
        if(x%2==0) {
            p*=p;
            x/=2;
        }
        else {
            res*=p;
            x--;
        }
    }
    return res;
}
ll cal(int x){
    int ra0=(n/2-x/2);
    int ra1=((n+1)/2-(x+1)/2);
    int rmi0=max(0,(n/2+1)/2-x/2);
    int rmi1=max(0,((n+1)/2+1)/2-(x+1)/2);
    //cout<<x<<' '<<ra0<<' '<<ra1<<' '<<rmi0<<' '<<rmi1<<' '<<flag0<<' '<<flag1<<' '<<k<<endl;
    ll tmp0,tmp1,tmp2;
    if(flag0){
        tmp0=Pow(ra1+rmi0);
    }
    else tmp0=0;
    if(flag1){
        tmp1=Pow(ra0+rmi1);
    }
    else tmp1=0;
    if(flag1&&flag0){
        tmp2=Pow(rmi1+rmi0);
    }
    else tmp2=0;
    //cout<<tmp1<<' '<<tmp0<<' '<<tmp2<<endl;
    return tmp1+tmp0-tmp2;
}
void update(int x){
    int mi0,mi1;
    if(n%2==0){
        mi0=n/2+1;
        mi1=n/2;
    }
    else {
        mi0=n/2+1;
        mi1=n/2+1;
    }
    if(x%2==0){
        if(x>mi0){
            if(ans[mi0*2-x]!=ans[x]) flag0=false;
        }
    }
    else {
        if(x>mi1){
            if(ans[mi1*2-x]!=ans[x]) flag1=false;
        }
    }
}
int main(){
    TelmaZzzz();
    //ios::sync_with_stdio(false);
    int t;
    R(t);
    ll k;
    int ti=0;
    while(t--){
        R(n,k);
        flag1=flag0=true;
        for(int i=1;i<=n;i++){
            ans[i]=0;
            bool tm1=flag1,tm0=flag0;
            update(i);
            ll tmp=cal(i);
            //cout<<tmp<<endl;
            if(k>tmp){
                k-=tmp;
                flag1=tm1;
                flag0=tm0;
                ans[i]=1;
                update(i);
            }
        }
        //cout<<k<<endl;
        if(k>1){
            printf("Case #%d: NOT FOUND!\n",++ti);
        }
        else {
            printf("Case #%d: ",++ti);
            for(int i=1;i<=n;i++){
                printf("%d",ans[i]);
            }
            puts("");
        }
    }
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值