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 l, r 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 l, r 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;
}
}