Codeforces Round #463 (Div. 1 + Div. 2, combined) B Recursive Queries

B. Recursive Queries

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
4
22 73 9
45 64 6
47 55 7
2 62 4
output
1
4
0
8
input
4
82 94 6
56 67 4
28 59 9
39 74 4
output
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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值