教主的集合序列

**

教主的集合序列

**

测试数据来自 system/1472

描述

定义集合S1为1到n之间所有正整数组成的集合,即S1={1,2,3…n}。当k>1时,Sk为集合Sk-1中任意两个不相同数之和的集合。

例如,当n=3时:
S1={1,2,3}
S2={3,4,5}
S3={7,8,9}
S4={15,16,17}
……

现将每个集合中所有元素取出,集合Si的数放在集合Si+1的数的前面,同一个集合数从小到大排序,这样得到一个序列L。例如,当n=3时,L=1,2,3,3,4,5,7,8,9,15,16,17……
那么,现对于给定的n和k,求L中第k个数。
格式
输入格式

输入包含1行,为2个正整数n和k,两个整数用逗号隔开。
输出格式

输出包含1行,为一个正整数,即序列L中第k个数。若这个数不存在,则输出-1。
样例1
样例输入1

4 6

Copy
样例输出1

4

Copy
限制

对于20%的数据,有k≤20;
对于40%的数据,有k≤10000,n≤5;
对于40%的数据,满足答案不超过2^31-1;
对于60%的数据,有k≤2^30-1;
对于80%的数据,有k≤10^100;
对于100%的数据,有k≤10^1000,1<n≤1000;且数据保证当n≤3时,k≤900000。

时限1s
提示

当n=4时,序列L=1,2,3,4,3,4,5,6,7,7……
所以序列中第6个数为4。

题解:
这条题目确实比较变态

首先要推出相关的公式,假设一个集合当中的元素有n个,得到相关的数学公式.这里我上网摘抄一下

Solution

显然的,每次得到的集合都会是区间
根据条件,每次区间会扩大一倍
第i个区间来讲
左端点为 2i−1

右端点为 2i∗n−2i−1+1

因而可以依次枚举每个区间的左右端点 加入高精度即可

其实,有一种更优秀的方式
可以发现,每个区间的大小可以被表达为
(n−3)∗2i−1+3

那么 前i个区间元素个数和也可以被计算
这就满足二分了

满足二分之后,再加上高精度的模板就万事大吉了

下面答案是vijos的解答

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
const int maxn=10005;/*精度位数,自行调整*/
//1.如果需要控制输出位数的话,在str()里面把len调成需要的位数
//2.很大的位数是会re的,所以如果是幂运算的话,如 计算x^p的位数n, n=p*log(10)x+1;(注意要加一)
//3.还可以加上qmul,取模的过程也就是str(),c_str()再搞一次
class bign
{
    //io*2 bign*5*2 bool*6
    friend istream& operator>>(istream&,bign&);
    friend ostream& operator<<(ostream&,const bign&);
    friend bign operator+(const bign&,const bign&);
    friend bign operator+(const bign&,int&);
    friend bign operator*(const bign&,const bign&);
    friend bign operator*(const bign&,int&);
    friend bign operator-(const bign&,const bign&);
    friend bign operator-(const bign&,int&);
    friend bign operator/(const bign&,const bign&);
    friend bign operator/(const bign&,int&);
    friend bign operator%(const bign&,const bign&);
    friend bign operator%(const bign&,int&);
    friend bool operator<(const bign&,const bign&);
    friend bool operator>(const bign&,const bign&);
    friend bool operator<=(const bign&,const bign&);
    friend bool operator>=(const bign&,const bign&);
    friend bool operator==(const bign&,const bign&);
    friend bool operator!=(const bign&,const bign&);
public:
    int len,s[maxn];
    bign()
    {
        memset(s,0,sizeof(s));
        len=1;
    }
    bign operator=(const char* num)
    {
        int i=0,ol;
        ol=len=strlen(num);
        while(num[i++]=='0'&&len>1)
            len--;
        memset(s,0,sizeof(s));
        for(i=0; i<len; i++)
            s[i]=num[ol-i-1]-'0';
        return *this;
    }
    bign operator=(int num)
    {
        char s[maxn];
        sprintf(s,"%d",num);
        *this=s;
        return *this;
    }
    bign(int num)
    {
        *this=num;
    }
    bign(const char* num)
    {
        *this=num;
    }
    string str() const
    {
        string res="";
        for(int i=0; i<len; i++)
            res=char(s[i]+'0')+res;
        if(res=="")
            res="0";
        return res;
    }
};
bool operator<(const bign& a,const bign& b)
{
    int i;
    if(a.len!=b.len)
        return a.len<b.len;
    for(i=a.len-1; i>=0; i--)
        if(a.s[i]!=b.s[i])
            return a.s[i]<b.s[i];
    return false;
}
bool operator>(const bign& a,const bign& b)
{
    return b<a;
}
bool operator<=(const bign& a,const bign& b)
{
    return !(a>b);
}
bool operator>=(const bign& a,const bign& b)
{
    return !(a<b);
}
bool operator!=(const bign& a,const bign& b)
{
    return a<b||a>b;
}
bool operator==(const bign& a,const bign& b)
{
    return !(a<b||a>b);
}
bign operator+(const bign& a,const bign& b)
{
    int up=max(a.len,b.len);
    bign sum;
    sum.len=0;
    for(int i=0,t=0;t||i<up; i++)
    {
        if(i<a.len)
            t+=a.s[i];
        if(i<b.len)
            t+=b.s[i];
        sum.s[sum.len++]=t%10;
        t/=10;
    }
    return sum;
}
bign operator+(const bign& a,int& b)
{
    bign c=b;
    return a+c;
}
bign operator*(const bign& a,const bign& b)
{
    bign res;
    for(int i=0; i<a.len; i++)
    {
        for(int j=0; j<b.len; j++)
        {
            res.s[i+j]+=(a.s[i]*b.s[j]);
            res.s[i+j+1]+=res.s[i+j]/10;
            res.s[i+j]%=10;
        }
    }
    res.len=a.len+b.len;
    while(res.s[res.len-1]==0&&res.len>1)
        res.len--;
    if(res.s[res.len])
        res.len++;
    return res;
}
bign operator*(const bign& a,int& b)
{
    bign c=b;
    return a*c;
}
//只支持大数减小数
bign operator-(const bign& a,const bign& b)
{
    bign res;
    int len=a.len;
    for(int i=0; i<len; i++)
    {
        res.s[i]+=a.s[i]-b.s[i];
        if(res.s[i]<0)
        {
            res.s[i]+=10;
            res.s[i+1]--;
        }
    }
    while(res.s[len-1]==0&&len>1)
        len--;
    res.len=len;
    return res;
}
bign operator-(const bign& a,int& b)
{
    bign c=b;
    return a-c;
}
bign operator/(const bign& a,const bign& b)
{
    int i,len=a.len;
    bign res,f;
    for(i=len-1; i>=0; i--)
    {
        f=f*10;
        f.s[0]=a.s[i];
        while(f>=b)
        {
            f=f-b;
            res.s[i]++;
        }
    }
    while(res.s[len-1]==0&&len>1)
        len--;
    res.len=len;
    return res;
}
bign operator/(const bign& a,int& b)
{
    bign c=b;
    return a/c;
}
bign operator%(const bign& a,const bign& b)
{
    int len=a.len;
    bign f;
    for(int i=len-1; i>=0; i--)
    {
        f=f*10;
        f.s[0]=a.s[i];
        while(f>=b)
            f=f-b;
    }
    return f;
}
bign operator%(const bign& a,int& b)
{
    bign c=b;
    return a%c;
}
bign& operator+=(bign& a,const bign& b)
{
    a=a+b;
    return a;
}
bign& operator-=(bign& a,const bign& b)
{
    a=a-b;
    return a;
}
bign& operator*=(bign& a,const bign& b)
{
    a=a*b;
    return a;
}
bign& operator/=(bign& a,const bign& b)
{
    a=a/b;
    return a;
}
bign& operator++(bign& a)
{
    a=a+1;
    return a;
}
bign& operator++(bign& a,int)
{
    bign t=a;
    a=a+1;
    return t;
}
bign& operator--(bign& a)
{
    a=a-1;
    return a;
}
bign& operator--(bign& a,int)
{
    bign t=a;
    a=a-1;
    return t;
}
istream& operator>>(istream &in,bign& x)
{
    string s;
    in>>s;
    x=s.c_str();
    return in;
}
ostream& operator<<(ostream &out,const bign& x)
{
    out<<x.str();
    return out;
}

int main(){
    int n,k,mask=1,l3;
    char s[maxn];
    bign l,tot,cur,b;
    scanf("%d %s",&n,s);
    tot = n; l = s; k = 1; b = 2;
    if(n==1){
        if(l==1) cout<<1;
        else cout<<-1;
        return 0;
    }else if(n==2){
        if(l<=3) cout<<l;
        else cout<<-1;
        return 0;
    }else if(n==3){
        l3 = 0;
        for(int i=l.len-1;i>=0;i--){
            l3 *= 10;
            l3 += l.s[i];
        }
        k = (l3-1)/3;
        b = 2; tot = 1;
        while(mask<=k){
            if(mask&k) tot *= b;
            b = b*b;
            mask <<= 1;
        }
        tot *= 2;
        tot = tot-2+l3-3*k;
        cout<<tot;
        return 0;
    }
    while(tot<l){
        ++k;
        tot += b*n-(b-1)*3;
        b *= 2;
    }
    k--; b /= 2;
    tot -= b*n-(b-1)*3;
    cout<<l-tot+b*2-2;
    return 0;
}

 

学会程序和算法,走遍天下都不怕

状态 耗时 内存占用

#1 Accepted 2ms 476.0 KiB
#2 Accepted 1ms 852.0 KiB
#3 Accepted 2ms 860.0 KiB
#4 Accepted 2ms 852.0 KiB
#5 Accepted 250ms 684.0 KiB
#6 Accepted 2ms 848.0 KiB
#7 Accepted 9ms 848.0 KiB
#8 Accepted 9ms 848.0 KiB
#9 Accepted 138ms 848.0 KiB
#10 Accepted 125ms 784.0 KiB

 

学会程序和算法,走遍天下都不怕

在这里插入图片描述大理苍山

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值