17997 Simple Counting Math

Source: Internet
Author: User

17997 Simple Counting

Time limit: 2000MS memory limit: 65535K
Number of submissions: 0 Number of Passes: 0

Question types: programming language: not Limited

Description
Ly is crazy about counting. Recently, he got a simple problem, but he had to learn gaoshu these days. So, he turns to your for help. You is given a sequence a with n positive integers numbered from 1 to N, and then expected to answer Q queries. Each queries contains a 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 is divided by (i-l+1).




Input format
The first line of the input was an integer T, indicates the number of test cases. Then T cases followed. For each test case:the first line contains the integers n, Q. The second line contains n positive integers A1, A2, .... An. The next Q line, all line contains, integers L, r.data range:1<= T <= 201 <= N, q <= 200001<= Ai < = 500001<= L <= R <= N



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



Input sample
25 21 2 3 4 51 53 56 310 7 3 6 24 111 32 55 6



Output sample
52231



Tips
Huge input, scanf is preferred for 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 = 0A3 MoD 1 = 0, A4 mod 2 = 0, A5 mod 3 ! = 0, so the answer is 2.

Given n numbers and M queries, each given interval [L, R], then ask a[l]% 1 = = 0? A[l + 1]% 2 = = 0?, statistical answer.

Idea: Consider fixed left endpoint L, that is, for each number a[i], first we already know that his position in the array is I.

Well, if K is his approximate. If you really ask if it is%k==0? , where should it start asking about this interval?

It should be in [I-k + 1,], because i-l + 1 = k. Get L equals this.

Then you can use a vector[l] to save, with L as the beginning of the interval to ask, those positions will be the ANS. Then the two points are less than or equal to the number of R.

#include <cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>#defineIOS Ios::sync_with_stdio (False)using namespacestd;#defineINF (0X3F3F3F3F)typedefLong Long intLL; #include<iostream>#include<sstream>#include<vector>#include<Set>#include<map>#include<queue>#include<string>Const intMAXN =20000+ -; Vector<int>POS[MAXN];voidWork () {intN, M; scanf ("%d%d", &n, &m);  for(inti =1; I <= N; ++i) {pos[i].clear (); }     for(inti =1; I <= N; ++i) {intx; scanf ("%d", &x); intEnd = (int) sqrt (x *1.0);  for(intj =1; J <= End; ++j) {if(x% J = =0) {                intL = I-j +1; if(L >=1) {pos[l].push_back (i); } Else  Break; if(x/j = = j)Continue; L= i-(x/j) +1; if(L >=1) {pos[l].push_back (i); }            }        }    }     for(inti =1; I <= N; ++i) {sort (Pos[i].begin (), Pos[i].end ()); }     for(inti =1; I <= m; ++i) {intL, R; scanf ("%d%d", &l, &R); intAns = Upper_bound (Pos[l].begin (), Pos[l].end (), R)-Pos[l].begin (); printf ("%d\n", ans); }}intMain () {#ifdef local freopen ("Data.txt","R", stdin);#endif    intT; scanf ("%d", &t);  while(t--) work (); return 0;}
View Code

17997 Simple Counting Math

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.