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. 无聊,纯css写了个评分鼠标移入的效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. java规范之checkstyle

    1. 概述 随着中心的代码规范的建立和实施,项目组对代码规范要求,以及软件工程师们对自身代码的编写规范重要性的认知,“代码规范”已经成为了中心的一个“热词”.然后怎么才能写出有规范的代码,怎么才能养成 ...

  3. json格式的一些常用操作方法

    package com.liveyc.restfull.until; import java.util.HashMap; import java.util.Iterator; import java. ...

  4. Metasploit 进阶

    本文是"T00LS Metasploit(第二季)"的文档版,是个人在观看视频动手操作的一个记录,仅供学习.文中会介绍Metasploit的一些基本使用:主要包括远程代码执行.MI ...

  5. 目标检测-基于Pytorch实现Yolov3(1)- 搭建模型

    原文地址:https://www.cnblogs.com/jacklu/p/9853599.html 本人前段时间在T厂做了目标检测的项目,对一些目标检测框架也有了一定理解.其中Yolov3速度非常快 ...

  6. Java Web Project Problems

    A: 项目红叉 1. 检验 Java Builder  Path 2. 检查 Projects Facets 3. 查看 Targets Runtimes B:项目红感叹号 1. 查看问题栏 Prob ...

  7. centos上git搭建

    1 git的安装需要一些包: yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-Ex ...

  8. spring源码解析--事务篇(前篇)

    对于每一个JAVA程序员,spring应该是再熟悉不过的框架了,它的功能有多强大我就不多说了,既然他有这么强大的功能,是如何实现的呢?这个就需要从他的原理去了解,而最直接了解原理的方式莫过于源码.当然 ...

  9. 使用亚马逊云服务器EC2做深度学习(二)配置Jupyter Notebook服务器

    这是<使用亚马逊云服务器EC2做深度学习>系列的第二篇文章. (一)申请竞价实例  (二)配置Jupyter Notebook服务器  (三)配置TensorFlow  (四)配置好的系统 ...

  10. SGU 206. Roads

    206. Roads time limit per test: 0.5 sec. memory limit per test: 65536 KB input: standard output: sta ...