Description

Let us define two functions f and g on positive integer numbers.

You need to process Q queries. In each query, you will be given three integers lr and k. You need to print the number of integers xbetween l and r inclusive, such that g(x) = k.

Input

The first line of the input contains an integer Q (1 ≤ Q ≤ 2 × 105) representing the number of queries.

Q lines follow, each of which contains 3 integers lr and k (1 ≤ l ≤ r ≤ 106, 1 ≤ k ≤ 9).

Output

For each query, print a single line containing the answer for that query.

Examples

input

output

input

output

Note

In the first example:

    • g(33) = 9 as g(33) = g(3 × 3) = g(9) = 9
    • g(47) = g(48) = g(60) = g(61) = 6
    • There are no such integers between 47 and 55.
    • g(4) = g(14) = g(22) = g(27) = g(39) = g(40) = g(41) = g(58) = 4

题意:

给定一个表达式,根据表达式求值。先输入q,代表有q次询问,接下来q行,分别为l,r,k。表示从l到r有多少个值为k的数字,输出一个整数

思路:

看数据量,q和l,r都很大,所以如果直接暴力会超时。所以要先求出每个数的值是多少。这里通过递归或者循环都能求出来,本人不习惯递归,使用循环求出。然后用二维数组,直接求出从1-i有多少个符合条件的数,二位数组的i代表从1到i有多少符合条件的,j代表的是结果,也就是k。所以求l到r之间等于k的就是ans[r][k] - ans[l-1][k]。具体看代码。

代码:

#include <bits/stdc++.h>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set> #define IO ios::sync_with_stdio(false);\
cin.tie();\
cout.tie(); typedef long long LL;
const long long INF = 0x3f3f3f3f;
const long long mod = 1e9+;
const double PI = acos(-1.0);
const int maxn = ;
const char week[][]= {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
const char month[][]= {"Janurary","February","March","April","May","June","July",
"August","September","October","November","December"
};
const int daym[][] = {{, , , , , , , , , , , , },
{, , , , , , , , , , , , }
};
const int dir4[][] = {{, }, {, }, {-, }, {, -}};
const int dir8[][] = {{, }, {, }, {-, }, {, -}, {, }, {-, -}, {, -}, {-, }}; using namespace std;
using namespace std; int n, k, ans[maxn][], q, l, r; int a[maxn];
void init()//将1-maxn的值求出来
{
for(int i=; i<=maxn; i++)
{
int sum=;
int ii=i;
while(ii)
{
if(ii%!=)
sum=sum*(ii%);
ii/=;
}
int sum1=;
while()
{
if(sum<)
{
a[i]=sum;
break;
}
while(sum)
{
if(sum%!=)
sum1=sum1*(sum%);
sum/=;
}
sum=sum1;
sum1=;
} }
} int main()
{
init();
for (int i = ; i <= maxn; i++)//将每个数的结果加上
{
ans[i][a[i]]++;
}
for (int i = ; i <= ; i++)
for (int j = ; j <= maxn; j++)//将从1到j的等于i的数求出来。
ans[j][i] += ans[j-][i];
cin >> q;
while (q--)
{
cin >> l>> r>> k;
cout <<ans[r][k] - ans[l-][k]<<endl;
}
}

最新文章

  1. 时间同步ntp服务的安装与配置(作为客户端的配置
  2. Android 敏感 API 的说明
  3. C#委托的介绍(delegate、Action、Func、predicate) --转载
  4. String Mybatis 多数据源配置
  5. Android多线程分析之二:Thread的实现
  6. AreYouBusy
  7. 06_在web项目中集成Spring
  8. ANDROID_MARS学习笔记_S02_009_Animation_Interpolator
  9. python获取当前路径几种方式
  10. MYSQL存储过程,清除指前缀的定表名的数据
  11. oracle 查询 归档日志最大值和平均值
  12. 自动提取文件系统---binwalk(一)
  13. Web API 入门 二 媒体类型
  14. delphi 动态绑定代码都某个控件
  15. 解压cpio.gz
  16. 调试工具DebugView
  17. RN组件备忘录
  18. curl以cookie的方式登录
  19. 2k8 32bit下载
  20. VS2015 python

热门文章

  1. jQuery经典面试题及答案精选[转]
  2. 简单高效的asp.net目录树源代码
  3. asp.net DataTable导出 excel的方法记录(第三方)
  4. 阿里云一键web环境包
  5. linux下C语言实现的哈希链表【转】
  6. MGR Switch Muti-Primary to single_primary
  7. centos如何设置定时任务
  8. 大数据系列之kafka监控kafkaoffsetmonitor安装
  9. VC RichEdit中英文关键字标红
  10. mybatis注解使用