传送门:

题面:

D. Mr. Panda and Geometric Sequence

time limit per test

3.0 s

memory limit per test

256 MB

input

standard input

output

standard output

Mr. Panda likes playing with numbers. One day he picked up a number 3612 and found it's interesting. 3612 can be divided into 3numbers 3, 6 and 12 which forms an integer geometric sequence.

An integer geometric sequence is a sequence of at least 3 positive integer numbers a0, a1, ..., an - 1, also there is a positive number D(D > 1) that for each i(0 ≤ i < n - 1), ai × D = ai + 1.

Mr. Panda named this kind of numbers "Happy Number". He also announced that leading zeros are forbidden, which means there should be no extra zeros before the numbers. Now Mr. Panda would like to know how many Happy Numbers are between L and R inclusive.

Input

The first line of the input gives the number of test cases, TT test cases follow. Each test case contains one line which consists of two numbers L and R.

  • 1 ≤ T ≤ 2 × 105.
  • 0 ≤ L ≤ R ≤ 1015.

Output

For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the number of Happy Numbers between L and R inclusive.

Example

input

Copy

4
123 124
468 470
248 248
124816 124832

output

Copy

Case #1: 1
Case #2: 1
Case #3: 1
Case #4: 1

Note

In the first test case, the only Happy Number between 123 and 124 is 124, in which 1 × 2 = 2 and 2 × 2 = 4.

In the second test case, the only Happy Number is 248.

In the third test case, the only Happy Number is 469, the common radio is .

In the fourth test case, the only Happy number between 124816 and 124832 is 124816, in which 1 × 2 = 2, 2 × 2 = 4, 4 × 2 = 8 and 8 × 2 = 16.

题意:

好数的定义为,你可以将这个数分为若干个部分,使得每一个部分可以构成一个等比数列。

现在给你若干个询问,问你在区间[L,R]中,好数的数量

题目分析:

首先我们设公比为,因为要满足一个数列为等比数列,则至少存在三个数,使得他们的公比相同,因此我们可以构造出这三个数分别为:

因为总的区间的上限为1e15,要满足至少存在三个数,使得他们成等比数列,则x,y,z的值的上限必定为1e5。因此我们可以考虑分别枚举p和q。又因为对于任意的p和q,可能又多个结果符合条件,因此我们仍然需要再枚举一个k,继而分别得到三个数。之后我们只需要将这三个数分别转化成原来的数,并将这个数存在一个桶中。

因为可能存在三个以上的数的等比数列,因此我们只需要对最后一个z不断乘上公比,并判断是否能够被p整除即可。

最后将桶里存的数进行排序并去重,对于每一个询问[l,r]只需要在桶里二分找到第一个大于r,以及第一个大于l-1的坐标,两式相减即为答案。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll>vec;
ll Pow[20];
int Count(ll n){//获取位数int cnt=0;while(n){n/=10;cnt++;}return cnt;
}
void init(){int n=1e5;Pow[0]=1;for(int i=1;i<=16;i++) Pow[i]=Pow[i-1]*10;for(int p=1;p<=n;p++){for(int q=p+1;q<=n/p;q++){//分别枚举p和qif(__gcd(p,q)>1) continue;//如果p和q不互质,则跳过判断for(int k=1;k<=n/p/q;k++){ll x=1ll*k*p*p,y=1ll*k*p*q,z=1ll*k*q*q;//构造三个数ll numx=Count(x),numy=Count(y),numz=Count(z);//分别获取三个数的位数ll numall=numx+numy+numz;if(numall>15) break;//如果总位数>15直接跳出ll now=z,res=x*Pow[numy+numz]+y*Pow[numz]+z;//将原来的数还原,并存到桶里vec.push_back(res);while(1){if(now%p!=0) break;//如果之后的数不能被p整除,则跳出//否则继续统计答案now=now*q/p;if(numall+Count(now)>15) break;numall+=Count(now);res=res*Pow[Count(now)]+now;vec.push_back(res);}}}}//排序并去重sort(vec.begin(),vec.end());vec.resize(unique(vec.begin(),vec.end())-vec.begin());
}
ll Sum(ll n){//二分求下标return upper_bound(vec.begin(),vec.end(),n)-vec.begin();
}
int main()
{int t;init();scanf("%d",&t);int Case=0;while(t--){ll l,r;scanf("%lld%lld",&l,&r);printf("Case #%d: %lld\n",++Case,Sum(r)-Sum(l-1));}return 0;
}

转载于:https://www.cnblogs.com/Chen-Jr/p/11007172.html

Gym 101775 D (思维)相关推荐

  1. Gym 102798A(思维) acm寒假集训日记21/12/31or22/1/1

    题目如下: AC代码如下: #include<iostream> #include<algorithm> #include<cstring> #include< ...

  2. I - Por Costel and the Pairs Gym - 100923I _思维啊——可惜我现在还没

    We don't know how Por Costel the pig arrived at FMI's dance party. All we know is that he did. The d ...

  3. 2017-2018 ACM-ICPC Asia East Continent League Final (ECL-Final) 题解(10 / 13)

    2017-2018 ACM-ICPC Asia East Continent League Final (ECL-Final) 9题金 6题银 5题铜 题目比较简单,但是读题太难了- 比赛链接:htt ...

  4. gym:Problem A Artwork(并查集思维题)

    20162017-acmicpc-nordic-collegiate-programming-contest-ncpc-2016 Problem A Artwork 题目链接 http://codef ...

  5. Gym - 101972H Beautiful Substrings(思维+模拟)

    题目链接:点击查看 题目大意:题目的意思挺难理解的..尤其是对我这种英语渣来说,简单说一下,就是先给出三个数字n,m,k,然后再给出两个字符串a和b,n和m代表的是字符串a和b的长度,然后描述题意: ...

  6. C - Mr. Panda and Strips Gym - 101194C(思维//尺取//2016 icpc china final)

    VJ地址 题意:选择一段or两段连续的区间,合成一段序列,使得选择的序列中没有相同的数字,求序列最长的长度 思路:由于是区间内不能有相同的数字,所以考虑用尺取,可以2*n的时间枚举第一段的长度,然后剩 ...

  7. H - Great Cells Gym - 101194H(数学推导/思维)

    VJ地址 ps:在训练时写这道题 差点被送走了 我们可以发现给出的公式可以转化这样 这个就不说了,就是k^(nm)所以排列的可能; 然后我们要重点观察这个公式,可以发现g * Ag(方案数 * goo ...

  8. 【Gym - 101775J】Straight Master(差分,思维)

    题干: A straight is a poker hand containing five cards of sequential rank, not necessarily to be the s ...

  9. 【2018icpc宁夏邀请赛现场赛】【Gym - 102222F】Moving On(Floyd变形,思维,离线处理)

    https://nanti.jisuanke.com/t/41290 题干: Firdaws and Fatinah are living in a country with nn cities, n ...

  10. 【Gym - 101061F】Fairness(dp,思维)

    题干: Dwik and his brother Samir both received scholarships from a famous university in India. Their f ...

最新文章

  1. 让CNN有了平移不变性,同时提升ImageNet成绩:Adobe开源新方法,登上ICML
  2. DIP第十章习题解答
  3. JFreeChart插件
  4. Spring Boot笔记-对dto数据传输对象及物联网公司主创建主键的认识
  5. 京东集团第二季度净营收创新高 刘强东松口气:未来一定逐步向好
  6. 【MySQL】MySQL 使用where条件的三种方式
  7. Flutter 即学即用系列博客——09 MethodChannel 实现原生与 Flutter 通信(二)
  8. HAR with Python WebDriver and BrowserMob Proxy
  9. Could not find artifact com.oracle:ojdbc7:pom:12.1.0.2 in central (https://r......的解决方案
  10. c语言实验二实验报告,C语言实验报告(二)
  11. vlookup使用步骤_vlookup函数的使用方法
  12. linux 用户shell 无效,浅谈Linux环境下,为什么设置用户ID位对shell脚本无效
  13. 【项目】森林预言家——森林火灾预防系统
  14. 记忆力减退之----SP3232---STM32
  15. 批量对下载的pdf文献重命名
  16. 在1-10中选择一个数,输出x+xx+xxx+xxx....x之和,如:数字为2,则2+22=24
  17. 关于当前若干主流网络仿真软件的综述及实例应用分析
  18. oracle 11g 闪回功能,Oracle 11g开启闪回功能Flashback
  19. 去除搜狗输入法弹窗骚扰的一个简易方法
  20. SQLDMO类在C#中的应用

热门文章

  1. UOS应用商店deb打包的正确目录结构
  2. SHELL中使用sed替换文本
  3. VS2010:外部依赖目录错误,怎么办
  4. 人工智能或可称之为人造智能
  5. 《TensorFlow深度学习应用实践》学习笔记1
  6. 白事碰上红事,徐渭应景吟诗
  7. 连文件搜索都不会用,也不自己反省一下?
  8. “重命名”用“改名”更好
  9. 服务器磁盘会影响应用么,想了解服务器磁盘的IO吞吐量,用sqlio 工具会不会对当前服务器有影响...
  10. python扩展取值范围_python 数据库取值范围内