玲珑学院OJ 1095 Six and One【暴力预处理+剪枝+二分查询】

224 篇文章 2 订阅
142 篇文章 0 订阅

1095 - Six and One

Time Limit:1s Memory Limit:128MByte

Submissions:128Solved:28

DESCRIPTION

Given the integers LL and RR, your task is calculating the count of the integer nn which meet the following conditions:

For instance, 11, 66, 1616, 3636, 6161, 37213721 are some integers which meet the conditions.

INPUT
The first line contains a positive integer TT, which represents there are TT test cases.The following is test cases. For each test case:The only line contains two positive integer LL and RR. 1T105,1LR10101≤T≤105,1≤L≤R≤1010
OUTPUT
For each test case, output in one line, contains one integer, which represents the count of the integers which meet the conditions.
SAMPLE INPUT
4
1 10
16 36
60 70
3720 3722
SAMPLE OUTPUT
2
2
2
1
题目大意:

ai是由6和1组成的数字,需要保证ai<=1e10

现在给你一个区间【l,r】,让你求一共有多少个N,使得L<=N<=R.

这里N表示是由若干个ai相乘得到的,N<=1e10


思路:


1、观察到T很大,明显是要考察O(1)查询或者是O(logn)查询的能力。

二分查询的考察肯定是比较套路的。

所以我萌需要进行预处理答案。


2、估计了一下,长度为1的有6和1,长度为2的有11 16 66 61.那么很明显,预处理出从长度1到长度为10的ai.一共也就是有2046个.

那么我们接下来考虑找寻组合,使得相乘得到的数小于1e10.

我们不妨在纸上简单的处理一些数据,不难发现,当取出来的数的个数大于等于4的时候,情况就变得非常少了。

那么我们Dfs暴力处理,做好剪枝,肯定不会超时。

这里需要注意几点:

①我们最多可能取很多数出来,6^12的得数是小于1e10的.所以我们最多可能要取12个数。

②我们还要对数组进行去重。

③Dfs过程中处理不当可能会出现爆LL的可能,所以我们在相乘两个数的时候,要在长度上进行预估。


3、接下来对于查询,我们找第一个大于l的数出现的位子posl,找第一个小于r的数出现的位子posr,预处理得到的可能的N一共有6w+数,直接枚举找位子肯定是要超时的,所以我们对这6W个数进行排序,然后二分这两个位子,那么ans=posr-posl;


Ac代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
ll ans[1000050];
ll anstmp[1000050];
int cnt;
void Dfs(ll nw,int len)
{
    ans[cnt++]=nw;
    if(len+1<=10)
    Dfs(nw*10+1,len+1);
    if(len+1<=10)
    Dfs(nw*10+6,len+1);
}
int lenn(ll a)
{
    int re=0;
    while(a)
    {
        a/=10;
        re++;
    }
    return re;
}
void Dfs_getnum(ll nw,int ned,int hav,int pos)
{
    if(ned==hav)
    {
        ans[cnt++]=nw;
        return ;
    }
    if(pos<=2046&&nw*ans[pos]>10000000000||lenn(nw)+lenn(ans[pos])>11)return ;
    for(int j=pos;j<=2046;j++)
    {
        if(nw*ans[j]>10000000000||lenn(nw)+lenn(ans[j])>11)break;
        else
        {
            Dfs_getnum(nw*ans[j],ned,hav+1,j);
        }
    }
}
void init()
{
    cnt=0;
    Dfs(1,1);
    Dfs(6,1);
    sort(ans,ans+cnt);
    ll test=1;
    for(int i=2;i<=13;i++)
    {
        for(int j=0;j<cnt;j++)
        Dfs_getnum(ans[j],i,1,j);
    }
    sort(ans,ans+cnt);
    for(int i=0;i<cnt;i++)anstmp[i]=ans[i];
    int tmpcnt=cnt;
    cnt=0;
    for(int i=0;i<tmpcnt;i++)
    {
        if(anstmp[i]==anstmp[i+1])continue;
        else
        {
            ans[cnt++]=anstmp[i];
        }
    }
}
int main()
{
    init();
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ll tmpl,tmpr;
        scanf("%lld%lld",&tmpl,&tmpr);
        int ansl=-1;
        int l=0;
        int r=cnt;
        while(r-l>=0)
        {
            int mid=(l+r)/2;
            if(ans[mid]>=tmpl)
            {
                ansl=mid;
                r=mid-1;
            }
            else l=mid+1;
        }
        int ansr=-1;
        l=0;
        r=cnt;
        while(r-l>=0)
        {
            int mid=(l+r)/2;
            if(ans[mid]<=tmpr)
            {
                ansr=mid;
                l=mid+1;
            }
            else r=mid-1;
        }
        if(ansl==-1||ansr==-1)
        {
            printf("0\n");
        }
        else printf("%d\n",ansr-ansl+1);
    }
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值