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. MYSQL进阶学习笔记十七:MySQL定期维护!(视频序号:进阶_36)

    知识点十八:MySQL定期维护(37) 一.Mysql的定时器 所谓的定时器,指的是在某个时间段去执行同样的代码.比如闹钟.每到指定的时间闹铃就会响.同样的,我们这个定时器,只要满足我们的一个定时条件 ...

  2. codeforces B. Marathon 解题报告

    题目链接:http://codeforces.com/problemset/problem/404/B 题目意思:Valera 参加马拉松,马拉松的跑道是一个边长为a的正方形,要求Valera从起点( ...

  3. linux应用之samba服务的安装及配置(centos)

    一.安装方式: 本文通过yum来重新进行Samba服务器的安装与配置. 二.Samba的简介: Samba是一个能让Linux系统应用Microsoft网络通讯协议的软件,而SMB是Server Me ...

  4. jQuery comet

    下面程序是例用从数据端推送信息,原理是每隔10秒读取一下data.txt文件,看有木有新的数据输入,如果有,则alert文件内容. hmtl代码是 <!DOCTYPE html> < ...

  5. css中块元素和行内元素区别

    行内元素特点 1.和其他元素都在一行上: 2.元素的高度.宽度.行高及顶部和底部边距不可设置: 3.元素的宽度就是它包含的文字或图片的宽度,不可改变. 块元素特点 1.每个块级元素都从新的一行开始,并 ...

  6. 【MongoDB学习-在.NET中的简单操作类】

    1.新建MVC项目, 管理NuGet包,进入下载MongDB.net库文件 2.新增项目DAL数据访问层,引用以下库文件: 3.C# 访问MongoDB通用方法类: using MongoDB.Dri ...

  7. 【转】看C++文档的小知识

    转载:http://www.ggv.com.cn/forum/clib/ctype/isspace.html 函数isspace 原型:extern int isspace(int c);    用法 ...

  8. BZOJ_2716_[Violet 3]天使玩偶&&BZOJ_2648_SJY摆棋子_KDTree

    BZOJ_2716_[Violet 3]天使玩偶&&BZOJ_2648_SJY摆棋子_KDTree Description 这天,SJY显得无聊.在家自己玩.在一个棋盘上,有N个黑色棋 ...

  9. HDU1150(最小顶点覆盖)

    Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  10. 想要删除table的某一行的js写法

    <tr><td onclick="del_tr(this)" >删除</td></tr> // 删除一行 function del_ ...