前缀和:CodeForces 932B Recursive Queries
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;
}
}
前缀和:CodeForces 932B Recursive Queries的更多相关文章
- 前缀和的应用 CodeForces - 932B Recursive Queries
题目链接: https://vjudge.net/problem/1377985/origin 题目大意就是要你把一个数字拆开,然后相乘. 要求得数要小于9,否则递归下去. 这里用到一个递归函数: i ...
- Codeforces 1117G Recursive Queries [线段树]
Codeforces 洛谷:咕咕咕 思路 设\(L_i,R_i\)为\(i\)左右第一个大于它的位置. 对于每一个询问\(l,r\),考虑区间每一个位置的贡献就是\(\min(r,R_i-1)-\ma ...
- Codeforces 932.B Recursive Queries
B. Recursive Queries time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- CodeForces - 940C + CodeForces - 932B (两道比较好的模拟题)
940C链接:http://codeforces.com/problemset/problem/940/C C. Phone Numbers time limit per test 2 seconds ...
- [Codeforces]817F. MEX Queries 离散化+线段树维护
[Codeforces]817F. MEX Queries You are given a set of integer numbers, initially it is empty. You sho ...
- sql script: Graphs, Trees, Hierarchies and Recursive Queries
--------------------------------------------------------------------- -- Inside Microsoft SQL Server ...
- sql server: Graphs, Trees, Hierarchies and Recursive Queries
--------------------------------------------------------------------- -- Chapter 09 - Graphs, Trees, ...
- [Codeforces 266E]More Queries to Array...(线段树+二项式定理)
[Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...
- 笔记-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 ...
随机推荐
- 【BZOJ】3524: [Poi2014]Couriers
[算法]主席树 [题解]例题,记录和,数字出现超过一半就递归查找. 主席树见[算法]数据结构 #include<cstdio> #include<algorithm> #inc ...
- 【CodeForces】889 C. Maximum Element 排列组合+动态规划
[题目]C. Maximum Element [题意]给定n和k,定义一个排列是好的当且仅当存在一个位置i,满足对于所有的j=[1,i-1]&&[i+1,i+k]有a[i]>a[ ...
- 【vijos】P1190 繁忙的都市
[算法]最小生成树 #include<cstdio> #include<algorithm> using namespace std; ; ]; int fa[maxn],he ...
- 在Unity中实现屏幕空间反射Screen Space Reflection(2)
traceRay函数 在上一篇中,我们有如下签名的traceRay函数 bool traceRay(float3 start, float3 direction, out float2 hitPixe ...
- TCP 传输控制协议(转)
开头先说几个协议: IP:网际协议 TCP:传输控制协议 Http:超文本传输协议 AMQP:高级消息队列协议 一:TCP是什么? TCP(Transmission Control Protocol ...
- XGBoost与LightGBM对比分析(转)
尊重原创 来源: https://blog.csdn.net/a790209714/article/details/78086867 XGBoost的四大改进: ①改进残差函数 不用Gini作为残 ...
- 回溯算法_01背包问题_Java实现
原文地址:http://blog.csdn.net/ljmingcom304/article/details/50314839 本文出自:[梁敬明的博客] 1.回溯算法 回溯算法也叫试探法,通俗的将就 ...
- jquery ajax的再次封装,简化操作
1.封装的ajax var funUrl="" // 每个请求地址相同的部分 function queryData(url,params,success,error){ url ...
- pip安装模块时:error: command 'gcc' failed with exit status 1
用安装python模块出现error: command 'gcc' failed with exit status 1 问题: gcc编译缺少模块 解决方法: yum install gcc libf ...
- Java集合里的一些“坑”
这里主要谈下Java集合在使用中容易被忽略.又容易出现的两个“坑”,一个是集合与数组互相转换,另一个是集合遍历删除.主要通过代码演示. 一.集合与数组互相转换中的“坑” //Test1.java pa ...