2017 ccpc 网络赛 hdu 6156

HDU - 6156

题意:如果x是k进制下的回文,那么x的权值为k,否则为1,求L-R的数在l-r进制下所有数的权值和

思路:取出L-1 和 R 在k进制下的前一半的数字,二分上限

AC代码:

#include "iostream"
#include "iomanip"
#include "string.h"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "set"
#include "map"
#include "algorithm"
#include "stdio.h"
#include "math.h"
#pragma comment(linker, "/STACK:102400000,102400000")
#define bug(x) cout<<x<<" "<<"UUUUU"<<endl;
#define mem(a,x) memset(a,x,sizeof(a))
#define step(x) fixed<< setprecision(x)<<
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define ll long long
#define endl ("\n")
#define ft first
#define sd second
#define lrt (rt<<1)
#define rrt (rt<<1|1)
using namespace std;
const ll mod=1e9+7;
const ll INF = 1e18+1LL;
const int inf = 1e9+1e8;
const double PI=acos(-1.0);
const int N=1e5+100;

bool check(int x, int odd, int y,int k){
    int tmp[105], t=0, re=0;
    while(x>0){
        tmp[++t]=x%k;
        x/=k;
    }
    for(int i=1; i<=t/2; ++i){
        swap(tmp[i], tmp[t-i+1]);
    }
    for(int i=1; i<=t; ++i){
        tmp[2*t-i+1-odd]=tmp[i];
    }
    for(int i=t*2-odd, bi=1; i>=1; --i){
        re+=tmp[i]*bi; bi*=k;
    } //cout<<re<<" YY "<<y<<endl;
    return re<=y;
}

int get_ans(int x, int k){
    int re=0, g, q=x, odd=0;
    int t=0,tmp[105];
    while(x>0){
        tmp[++t]=x%k;
        x/=k;
    }
    for(int i=1; i<=t/2; ++i){
        swap(tmp[i], tmp[t-i+1]);
    }
    for(int i=t+1>>1, bi=1; i>=1; --i){
        re+=tmp[i]*bi; bi*=k;
    }
    if(t&1) odd=1; //cout<<re<<endl;
    int l=0, r=re, ans=0;
    while(l<=r){
        int mid=l+r>>1;
        if(check(mid, odd, q, k)){
            ans=mid;
            l=mid+1;
        }
        else r=mid-1;
    }
    int bi=1; //cout<<(t+1>>1)<<endl;
    for(int i=2; i<=t+1>>1; ++i){
        bi*=k;
    } //cout<<t<<endl;
    if(t&1 || t==0){
         bi-=1;
         ans-=bi, ans+=bi*2;
    }
    else{
        bi-=1;
        ans-=bi, ans+=(bi+1)*(k-1);
        ans+=bi*2;//cout<<bi<<endl;
    } //cout<<ans<<endl;
    return ans+1;
}

int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    ll L,R,l,r;
    int T,cas=0; cin>>T;
    while(T--){
        ll ans=0;
        cin>>L>>R>>l>>r;
        for(int i=l; i<=r; ++i){
            ll tl, tr;
            tl=get_ans(L-1, i);
            tr=get_ans(R, i);
            ans+=((tr-tl)*i);
            ans+=(R-L+1)-(tr-tl);
        }
        cout<<"Case #"<<++cas<<": ";
        cout<<ans<<endl;
    }
    return 0;
}

 

posted on 2017-08-20 17:17  lazzzy  阅读(333)  评论(3编辑  收藏  举报

导航