17997 Simple Counting

时间限制:2000MS  内存限制:65535K
提交次数:0 通过次数:0

题型: 编程题   语言: 不限定

Description

Ly is crazy about counting . Recently , he got a simple problem , but he had to learn Gaoshu these days .So , he turns to you for help .
You are given a sequence A with n positive integers numbered from 1 to n , and then expected to answer Q queries .
Each queries contains an interval [L,R] , you should find the number of index i which satisfies :
{i | Ai mod (i-L+1) = 0 , L <= i <= R }

where Ai mod (i-L+1) = 0 means that Ai can be divided by (i-L+1) .

输入格式

The first line of the input is an integer T , indicates the number of test cases .
Then T cases followed. For each test case :
The first line contains two integers n, Q .
The second line contains n positive integers A1, A2, …. An .
The next Q line , each line contains two integers L, R. Data range :
1<= T <= 20
1 <= n, Q <= 20000
1<= Ai <= 50000
1<= L <= R <= n

输出格式

For each query, output a single line with an integer indicates the answer expected .

输入样例

2
5 2
1 2 3 4 5
1 5
3 5 6 3
10 7 3 6 24 11
1 3
2 5
5 6

输出样例

5
2
2
3
1

提示

Huge input, scanf is preferred for C/C++.

During first sample ,
for the first query ,A1 mod 1 = 0 , A2 mod 2 = 0 , A3 mod 3 = 0 , A4 mod 4 = 0 ,A5 mod 5 = 0 , so the answer is 5 ;
for the second query , A3 mod 1 = 0 , A4 mod 2 = 0 , A5 mod 3 != 0 , so the answer is 2 .

给定n个数字和m次询问,每次给定区间[L, R],然后问a[L] % 1 == 0?  a[L + 1] % 2 == 0?,统计答案。

思路:考虑固定左端点L, 就是,对于每一个数字a[i],首先我们已经知道他在数组里的位置是i。

那么如果k是他的约数的话。如果真有询问问到它是否%k==0?,那么这个时候它应该从哪里开始这段区间的询问呢?

应该要在[i - k + 1, ]处吧,因为i - L + 1 = k。得到L等于这个。

然后就可以用一个vector[L]来保存,以L为开始的区间询问,那些位置会是得到ans的。然后二分小于等于R的个数就可以了。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = + ;
vector<int>pos[maxn];
void work() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = ; i <= n; ++i) {
pos[i].clear();
}
for (int i = ; i <= n; ++i) {
int x;
scanf("%d", &x);
int end = (int)sqrt(x * 1.0);
for (int j = ; j <= end; ++j) {
if (x % j == ) {
int L = i - j + ;
if (L >= ) {
pos[L].push_back(i);
} else break;
if (x / j == j) continue;
L = i - (x / j) + ;
if (L >= ) {
pos[L].push_back(i);
}
}
}
}
for (int i = ; i <= n; ++i) {
sort(pos[i].begin(), pos[i].end());
}
for (int i = ; i <= m; ++i) {
int L, R;
scanf("%d%d", &L, &R);
int ans = upper_bound(pos[L].begin(), pos[L].end(), R) - pos[L].begin();
printf("%d\n", ans);
}
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}

17997 Simple Counting 数学的更多相关文章

  1. Codeforces 911D. Inversion Counting (数学、思维)

    题目链接:Inversion Counting 题意: 定义数列{ai|i=1,2,...,n}的逆序对如下:对于所有的1≤j<i≤n,若ai<aj,则<i,j>为一个逆序对. ...

  2. TOJ 1258 Very Simple Counting

    Description Let f(n) be the number of factors of integer n. Your task is to count the number of i(1 ...

  3. ACM学习历程—HDU5490 Simple Matrix (数学 && 逆元 && 快速幂) (2015合肥网赛07)

    Problem Description As we know, sequence in the form of an=a1+(n−1)d is called arithmetic progressio ...

  4. 「10.8」simple「数学」·walk「树上直径」

    A. Simple 本来以为很难,考场瞎推了推好像会了...... 想起小凯的诱惑,迷?? 首先$n$,$m$,$q$同除$gcd(n,m)$,显然$q$以内的数假如不是$gcd$的倍数,那么一定不能 ...

  5. UVA 11401 - Triangle CountingTriangle Counting 数学

    You are given n rods of length 1,2, . . . , n. You have to pick any 3 of them and build a triangle. ...

  6. [CSP-S模拟测试]:Simple(数学)

    题目描述 对于给定正整数$n,m$,我们称正整数$c$为好的,当且仅当存在非负整数$x,y$,使得$n\times x+m\times y=c$. 现在给出多组数据,对于每组数据,给定$n,m,q$, ...

  7. zoj 3286 Very Simple Counting---统计[1,N]相同因子个数

    Very Simple Counting Time Limit: 1 Second      Memory Limit: 32768 KB Let f(n) be the number of fact ...

  8. 13 Stream Processing Patterns for building Streaming and Realtime Applications

    原文:https://iwringer.wordpress.com/2015/08/03/patterns-for-streaming-realtime-analytics/ Introduction ...

  9. [C5] Andrew Ng - Structuring Machine Learning Projects

    About this Course You will learn how to build a successful machine learning project. If you aspire t ...

随机推荐

  1. 启动jmeter报错

    启动jmeter.bat时报错

  2. MPEG学习

    Mpeg:moving picture experts group 移动图片专家组 导入:Mpeg技术在我理解就是我们对音视频信息的一个输出标准.主要包括MPEG-1.MPEG-2.MPEG-4.MP ...

  3. MySQL丨分页查询

    直奔主题 一.sql语句:select * from table limit startPageNum,everyPageNum 1)语句解析: table:你要查询的表 startPageNum:从 ...

  4. CodeForces813E:Army Creation (主席树---上一题的加强版)

    As you might remember from our previous rounds, Vova really likes computer games. Now he is playing ...

  5. robotium

    Test run failed: Permission Denial: starting instrumentation ComponentInfo{android.support.v7.appcom ...

  6. AutoIT:界面与自动化操作结合来简化日常劳动: .Net Reactor验证License,设置License,创建License,截图AutoIt自动化实现。(七)

    版本六中存在一个显著问题是: 当exe文件生存之后,运行的时候,通过consoleWrite函数打印出来的数据是无法展示出来的.这就存在一个问题:当运行失败的时候,我还是看不到任何log信息. 于是, ...

  7. rsync(四)技术报告

    1.1 摘要 本报告介绍了一种将一台机器上的文件更新到和另一台机器上的文件保持一致的算法.我们假定两台机器之间通过低带宽.高延迟的双向链路进行通信.该算法计算出源文件中和目标文件中一致的部分(译者注: ...

  8. 表单提交Post方法、Get方法

     表单用来接受用户的输入,并将用户的输入以“name=value值对”集合的形式提交到服务器进行处理.那么表单是怎样将数据提交到服务器的?服务器是怎样对表单数据进行处理的?下面我将为大家揭开表单提交背 ...

  9. unittest执行测试用例的N种姿势总结

    1.我们写几个方法,用来做测试用例 2.我们在另一文件中引用这个模块下面的所有类方法,先看第一种方法: 运行结果: 缺点:每个用例都需要加载到测试套件中,如果有1000个用例,要写1000次重复的代码 ...

  10. 生产环境下JAVA进程高CPU占用故障排查---temp

    问题描述:生产环境下的某台tomcat7服务器,在刚发布时的时候一切都很正常,在运行一段时间后就出现CPU占用很高的问题,基本上是负载一天比一天高. 问题分析:1,程序属于CPU密集型,和开发沟通过, ...