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;
}
}

前缀和:CodeForces 932B Recursive Queries的更多相关文章

  1. 前缀和的应用 CodeForces - 932B Recursive Queries

    题目链接: https://vjudge.net/problem/1377985/origin 题目大意就是要你把一个数字拆开,然后相乘. 要求得数要小于9,否则递归下去. 这里用到一个递归函数: i ...

  2. Codeforces 1117G Recursive Queries [线段树]

    Codeforces 洛谷:咕咕咕 思路 设\(L_i,R_i\)为\(i\)左右第一个大于它的位置. 对于每一个询问\(l,r\),考虑区间每一个位置的贡献就是\(\min(r,R_i-1)-\ma ...

  3. Codeforces 932.B Recursive Queries

    B. Recursive Queries time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  4. CodeForces - 940C + CodeForces - 932B (两道比较好的模拟题)

    940C链接:http://codeforces.com/problemset/problem/940/C C. Phone Numbers time limit per test 2 seconds ...

  5. [Codeforces]817F. MEX Queries 离散化+线段树维护

    [Codeforces]817F. MEX Queries You are given a set of integer numbers, initially it is empty. You sho ...

  6. sql script: Graphs, Trees, Hierarchies and Recursive Queries

    --------------------------------------------------------------------- -- Inside Microsoft SQL Server ...

  7. sql server: Graphs, Trees, Hierarchies and Recursive Queries

    --------------------------------------------------------------------- -- Chapter 09 - Graphs, Trees, ...

  8. [Codeforces 266E]More Queries to Array...(线段树+二项式定理)

    [Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...

  9. 笔记-Recursive Queries

    Recursive Queries \[m_{l,r}=\textrm{id}(\max_{i=l}^r a_i)\\ f(l,r)= \begin{cases} (r-l+1)+f(l,m_{l,r ...

随机推荐

  1. 【洛谷】3375 KMP字符串匹配

    [算法]KMP [题解][算法]字符串 #include<cstdio> #include<algorithm> #include<cstring> using n ...

  2. Tornado/Python 学习笔记(二)

    部分ssrpc.py代码分析 -- 服务端: 1 #!/usr/bin/python3 2 3 from xmlrpc.client import Fault, dumps, loads 4 impo ...

  3. 选择问题(选择数组中第K小的数)

    由排序问题可以引申出选择问题,选择问题就是选择并返回数组中第k小的数,如果把数组全部排好序,在返回第k小的数,也能正确返回,但是这无疑做了很多无用功,由上篇博客中提到的快速排序,稍稍修改下就可以以较小 ...

  4. skb_reserve(skb,2)中的2的意义

    skb_reserve() skb_reserve()在数据缓存区头部预留一定的空间,通常被用来在数据缓存区中插入协议首部或者在某个边界上对齐.它并没有把数据移出或移入数据缓存区,而只是简单地更新了数 ...

  5. count(*)与count(1)、count('xxx')等在使用语法方面的区别

    语法方面: 区别就是:没有区别!!! “*”号是通配符: “*”号是通配符 “*”号是通配符 使用"*"号和使用其他数字和任意非字段字符在使用方面没有任何语法错误; 至于效率方面是 ...

  6. Mybatis Common Mapper文件

    表名/条件/字段 都可以传入进去 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mappe ...

  7. Django 1.10文档中文版Part1

    目录 第一章.Django1.10文档组成结构1.1 获取帮助1.2 文档的组织形式1.3 第一步1.4 模型层1.5 视图层1.6 模板层1.7 表单1.8 开发流程1.9 admin站点1.10 ...

  8. linux nginx php-fpm被攻击

    1.nginx错误日志:报错 2018/05/30 16:30:55 [error] 8765#0: *1485 connect() to unix:/tmp/php-70-cgi.sock fail ...

  9. cgic实现输入文件名,打开文件的功能

    a.c文件 #include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdarg. ...

  10. AXFR和IXFR区域传输及原理

    由于区域在DNS中发挥着重要的作用,因此希望在网络上的多个DNS服务器中提供区域,以提供解析名称查询时的可用性和容错.否则,如果使用单个服务器而该服务器没有响应,则该区域中的名称查询会失败.对于主要区 ...