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.
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).
For each query, print a single line containing the answer for that query.
4 22 73 9 45 64 6 47 55 7 2 62 4
1 4 0 8
4 82 94 6 56 67 4 28 59 9 39 74 4
3 1 1 5
题意:f(n)函数功能:求出n的各数位的乘积,同时自动忽略0,例如f(102)=2,即f(n) !=0,g(n)题意已明确。现在给你一个区间l,r,一个数字k,问你区间l,r内的数字i有几个g(i)=k。
思路:区间大小1e6,k只有9个,所以前缀和打表即可。
时间复杂度:前缀和打表+在线查询=O(1e6*9)+O(1e5*2)。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int ans[10][1000005],q,l,r,k;
int op(int x) //f(n)函数的功能实现
{
if(x<10)return x;
int tmp,as;
while(1)
{
as=1;
while(x)
{
tmp=x%10;
x/=10;
if(tmp)as*=tmp;
}
if(as<10)return as;
x=as;
}
}
int main()
{
for(int i=1;i<=9;i++)ans[i][0]=0;
for(int i=1;i<=9;i++) //前缀和打表
{
for(int j=1;j<=1000000;j++)
{
if(op(j)==i){ans[i][j]=ans[i][j-1]+1;}
else ans[i][j]=ans[i][j-1];
}
}
scanf("%d",&q);
while(q--) //在线查询
{
scanf("%d%d%d",&l,&r,&k);
printf("%d\n",ans[k][r]-ans[k][l-1]);
}
return 0;
}