Summer exams 7: number (half + number of combinations)

topic:

 

 

 analysis:

The meaning of problems: seeking n + 1 ~ n * 2 m in exactly n k has a binary 1, and the number.

By playing table output is the number of bits in each number 1 can be found: a greater number, which corresponds to the interval containing 2 1, 3 1 ...... larger the number of

In other words, the answer to meet the monotonicity can be half found just a few = k (xx = check (mid * 2 ) -check (mid)).

Solving Number: If find a minimum of n satisfy the condition, then n + 1, n + 2 ...... also likely to meet the conditions, so that the answer must be to meet the conditions for a continuous range.

Using two binary, the first determined smallest n, find the maximum of the second n1, n ~ n1 is the number of this number.

How to calculate the number of satisfying a number of how many there are k 1 in binary?

From low to high for each bit of this number, if this bit is 1, then the number of combinations is calculated by: position 0 selected from (~ i-1 bits of all K-CNT ) a number of programs (CNT already With 1)

Because the back position has been cnt number 1, then the remaining position selected from several k-1 make up. (This calculates the number of 1 ~ n in number satisfying the condition, then the resulting n + 1 ~ 2 * n by subtraction)

Note: The number of combinations to use recursive initialization of Pascal's Triangle, otherwise it will explode or long long time out

 

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll inf=(ll) 1 << 62 ;
ll k,c[70][70];
ll quick_pow(ll a,ll k)
{
    ll ans=1;
    while(k){
        if(k&1) ans*=a;
        a*=a; k>>=1;
    }
    return ans;
}
void init()
{
    for(int i=0;i<=63;i++) c[i][0]=1;
    for(int i=1;i<=63;i++)
     for(int j=1;j<=63;j++)
      c[i][j]=c[i-1][j]+c[i-1][j-1];
}
ll check(ll x)
{
    //printf("x:%lld ",x);
    ll tmp=0,cnt=0;
    for(int i=62;i>=0;i--)
     if((x>>i)&1){
         if(k>=cnt) tmp+=c[i][k-cnt];
        cnt++;
    }
    return tmp;
}
int main()
{
    freopen("number.in","r",stdin);
    freopen("number.out","w",stdout);
    int T; ll m;
    scanf("%d",&T);
    init();
    while(T--){
        scanf("%lld%lld",&m,&k);
        if(k==1) { printf("4 -1\n"); continue; }
        ll l=1,r=inf,ans1=1,ans2=1;
        while(l<r){
            ll mid=(l+r)>>1,xx=check(mid*2)-check(mid);
            if(xx<m) l=mid+1;
            else if(xx>m) r=mid;
            else ans1=mid,r=mid;
        }
        l=1,r=inf;
        while(l<r){
            ll mid=(l+r)>>1,xx=check(mid*2)-check(mid);
            if(xx<m) l=mid+1;
            else if(xx>m) r=mid;
            else ans2=mid,l=mid+1;
        }
        printf("%lld %lld\n",ans1,ans2-ans1+1);
    }
}
/*
3
3 2

4
0 2
1 2
2 2
3 2


*/

 

Guess you like

Origin www.cnblogs.com/mowanying/p/11431364.html