问题 1110: 2^k进制数

问题 1110: 2^k进制数

时间限制: 1Sec 内存限制: 128MB 提交: 812 解决: 375

题目描述

​ 设r是个2^k 进制数,并满足以下条件:
(1)r至少是个2位的2^k 进制数。
(2)作为2^k 进制数,除最后一位外,r的每一位严格小于它右边相邻的那一位。
(3)将r转换为2进制数q后,则q的总位数不超过w。
在这里,正整数k(1≤k≤9)和w(k〈w≤30000)是事先给定的。

问:满足上述条件的不同的r共有多少个?
我们再从另一角度作些解释:设S是长度为w 的01字符串(即字符串S由w个“0”或“1”组成),S对应于上述条件(3)中的q。将S从右起划分为若干个长度为k 的段,每段对应一位2k进制的数,如果S至少可分成2段,则S所对应的二进制数又可以转换为上述的2k 进制数r。
例:设k=3,w=7。则r是个八进制数(2^3=8)。由于w=7,长度为7的01字符串按3位一段分,可分为3段(即1,3,3,左边第一段只有一个二进制位),则满足条件的八进制数有:
2位数:高位为1:6个(即12,13,14,15,16,17),高位为2:5个,…,高位为6:1个(即67)。共6+5+…+1=21个。
3位数:高位只能是1,第2位为2:5个(即123,124,125,126,127),第2位为3:4个,…,第2位为6:1个(即167)。共5+4+…+1=15个。
所以,满足要求的r共有36个。

输入

​ 只有1行,为两个正整数,用一个空格隔开:
k w

输出

​ 1行,是一个正整数,为所求的计算结果,即满足条件的不同的r的个数(用十进制数表示),要求最高位不得为0,各数字之间不得插入数字以外的其他字符(例如空格、换行符、逗号等)。
(提示:作为结果的正整数可能很大,但不会超过200位)

样例输入

3  7

样例输出

36

提示

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
using namespace std;
const int N=60;
const int Mx=500;
const int MAXN=600+10;
struct hp
{
    int num[N];
    hp & operator = (const char*);
    hp & operator = (int);
    hp();
    hp(int);
    hp operator + (const hp &) const;
    hp operator - (const hp &) const;
    hp operator * (const hp &) const;
    hp operator / (const hp &) const;
    bool operator > (const hp &) const;
    bool operator <= (const hp &) const;
    hp & operator += (const hp &);

};
hp Cc[520];
hp one;
bool hp::operator > (const hp &b) const  //比较
{
    if(num[0]!=b.num[0]) return num[0]>b.num[0];
    for(int i=num[0];i>=1;i--)
    {
        if(num[i]!=b.num[i])
            return (num[i]>b.num[i]);
    }
    return false;
}
hp & hp::operator +=(const hp &b) {return *this=*this+b;}
bool hp::operator <= (const hp &b) const {return !(*this>b);}
hp & hp::operator = (const char *c)
{
    memset(num,0,sizeof(num));
    int n=strlen(c),j=1,k=1;
    for(int i=1;i<=n;i++)
    {
        if(k==10000) j++,k=1;
        num[j]+=k*(c[n-i]-'0');
        k*=10;
    }
    num[0]=j;
    return *this;
}
hp & hp::operator = (int a)
{
    char s[Mx];
    sprintf(s,"%d",a);
    return *this=s;
}
hp::hp() {memset(num,0,sizeof(num));num[0]=1;}
hp::hp(int n) {*this=n;}
hp hp::operator +(const hp &b) const  //加法
{
    hp c;
    c.num[0]=max(num[0],b.num[0]);
    for(int i=1;i<=c.num[0];i++)
    {
        c.num[i]+=num[i]+b.num[i];
        if(c.num[i]>=10000)
        {
            c.num[i]-=10000;
            c.num[i+1]+=1;
        }
    }
    if(c.num[c.num[0]+1]>0) c.num[0]++;
    return c;
}
hp hp::operator - (const hp &b) const  //减法
{
    hp c;
    c.num[0]=num[0];
    for(int i=1;i<=c.num[0];i++)
    {
        c.num[i]+=num[i]-b.num[i];
        if(c.num[i]<0)
        {
            c.num[i]+=10000;
            c.num[i+1]--;
        }
    }
    while(c.num[c.num[0]]==0&&c.num[0]>1) c.num[0]--;
    return c;
}
hp hp::operator * (const hp &b) const  //乘法
{
    hp c;
    c.num[0]=num[0]+b.num[0]+1;
    for(int i=1;i<=num[0];i++)
    {
        for(int j=1;j<=b.num[0];j++)
        {
            c.num[i+j-1]+=num[i]*b.num[j];
            c.num[i+j]+=c.num[i+j-1]/10000;
            c.num[i+j-1]%=10000;
        }
    }
    while(c.num[c.num[0]]==0&&c.num[0]>1) c.num[0]--;
    return c;
}
hp hp::operator / (const hp& b) const  //二分优化除法
{
    hp c,d;
    c.num[0]=num[0]+b.num[0]+1;
    d.num[0]=0;
    for(int i=num[0];i>=1;i--)
    {
        memmove(d.num+2,d.num+1,sizeof(d.num)-sizeof(int)*2);
        d.num[0]++;
        d.num[1]=num[i];
        int l=0,r=9999,mid;
        while(l<r)
        {
            mid=(l+r)>>1;
            if(b*hp(mid)<=d) l=mid+1;
            else r=mid;
        }
        c.num[i]=r-1;
        d=d-b*hp(r-1);
    }
    while(c.num[c.num[0]]==0&&c.num[0]>1) c.num[0]--;
    return c;
}
ostream & operator << (ostream & o,hp &n)  //输出
{
    o<<n.num[n.num[0]];
    for(int i=n.num[0]-1;i>=1;i--)
    {
        o.width(4);
        o.fill('0');
        o<<n.num[i];
    }
    return o;
}
int main()
{
    one=1;
    int k,w;
    cin>>k>>w;
    int x1=w/k;
    int x2=w%k;
    int M=(1<<k)-1;
    hp ans;
    hp b,v;
    Cc[0]=1;
    for(int i=1;i<=min(x1,M);i++){  //预处理了∑ C(2^k-1,i)(2<=i<=w/k) ,
        b=(M-i+1);
        v=i;
        Cc[i]=Cc[i-1]*b/v;
    }
    for(int i=2;i<=min(x1,M);i++)
    {
        ans=ans+Cc[i];
    }
    int M2=(1<<x2)-1;
    Cc[M]=Cc[min(x1,M)];
    for(int i=1;i<=M2;i++)
        {
            b=(M-i+1-x1);
            v=(M-i+1);
            Cc[M-i]=Cc[M-i+1]*b/v; //倒序计算∑ C(2^k-1-val,w/k)(1<=val<=2^(w mod k)-1) ,通过后一个数推前一个数。
            ans=ans+Cc[M-i];
        }
    cout<<ans<<endl;
    return 0;
}

思路

高精度压位计算。 重载了+,-,*,/;
模板参考:https://www.cnblogs.com/hoskey/p/3722416.html

摘自Mr.black的解析:https://www.cnblogs.com/Blacko/p/3380675.html

作为2^k 进制数,除最后一位外,r的每一位严格小于它右边相邻的那一位。

其实这是在暗示组合数,

显然r中的不会有相同的位

如果每一位都不同,显然只有严格递增的排列是合法的

这便是组合- =,

将 r 转化成这种形式(设k为3) 000 000 000 000

显然除首位外每一位的取值范围为 000 to 111(2^k-1)

在首位为0的情况下,最多可取 w/k 位,且题目要求大于2位,

则在首位为0的合法解有 ∑ C(2^k-1,i)(2<=i<=w/k) ,

Ps. 如果 w 模 k 等于 0 仅考虑上述情况即可。

考虑首位不为0的情况

显然首位不为0的话,r 就有 w/k+1 位,

除首位外还有w/k位,

可以枚举首位的取值范围为 1 to 2^(w mod k)-1

设首位取值为 val

则剩下 w/k 位 取值范围为 val+1 to 2^k-1

也就是有 2^k-1-val 个数可取

所以首位不为0的合法解有 ∑ C(2k-1-val,w/k)(1<=val<=2(w mod k)-1)

所以上述两者相加便是正解(需要高精运算)

神犇的解法更简单高效。

Ps: 涉及高精度的计算,若是高精与低精度的数值运算,都要先把低转为高。

posted @ 2019-12-05 17:02  Anticlock  阅读(463)  评论(0编辑  收藏  举报