Gym 101102J --- Divisible Numbers, strong heart 101102

Source: Internet
Author: User

Gym 101102J --- Divisible Numbers, strong heart 101102

Question Link

Http://codeforces.com/gym/101102/problem/J

 

Description

Standard input/output

You are given an arrayAOf integers of sizeN, AndQQueries. For each query, you will be given a set of distinct integersSAnd two integersLAndRThat represent a range in the array. Your task is to count how many numbers in the given range are divisible by at least one number from the set.

Input

The first line of input contains a single integerT, The number of test cases.

The first line of each test case contains two integers,NAndQ(1 digit ≤ DigitN, Bytes,QLimit ≤ limit 105), the size of the array and the number of queries, respectively.

The next line containsNSpace-separated integers, the values of the arrayA(1 digit ≤ DigitAILimit ≤ limit 109 ).

Each of the nextQLines contain the description of one query in the form:

LRS

WhereLAndR(1 digit ≤ DigitLLimit ≤ limitRLimit ≤ limitN) Represent the range, andSIs an integer between 1 and 1023 (aggressive) and represents the set; consider the binary representation of the numberS, IfIThBit (1-based) is 1, then the numberIBelongs to the set. Since S is less than1024, the values in the set are between 1 and 10.

For example: ifSIs equal to 6, the binary representation of 6 is 110, and this means the values in the set are 2 and 3.

The input was given in this way to reduce the size of the input file.

Output

Print the answer for each query on a single line.

Sample Input

Input
1
4 2
2 5 3 8
1 3 2
2 4 355
Output
1
3

Question: Input n, q represents a sequence composed of n numbers. q queries. Input l r s for each query represents a set, and the position of 1 in the binary of s, for example: 6 (10) = 110 (2) corresponds to the set {2, 3}, and now calculates the range l ~ R: The number of divisible data in a set (any one;

Idea: Because the s value range is 1 ~ 1023 when n numbers are input, the system will directly find the s (corresponding set number) that can be divisible, and then use the prefix and representation. Then, each query will be 0 (1;

The Code is as follows:
# Include <iostream> # include <algorithm> # include <cstdio> # include <cstring> # include <cmath> using namespace std; typedef long LL; const int MAXN = 1e5 + 5; int sum [512] [MAXN]; template <class T> inline void Scan (T & ret) {char c = getchar (); while (c <'0' | c> '9') c = getchar (); ret = c-'0'; while (c = getchar (), c> = '0' & c <= '9') ret = ret * 10 + (c-'0');} int main () {int T; scan (T); while (T --) {int n, q, x, s; Scan (n); Scan (Q); // memset (sum, 0, sizeof (sum); add Timeout !!! For (int I = 1; I <= n; I ++) {s = 0; Scan (x); for (int j = 2; j <= 10; j ++) if (x % j = 0) s = s | (1 <(J-2); for (int j = 0; j <512; j ++) sum [j] [I] = sum [j] [I-1] + (s & j )? 1:0) ;}while (q --) {int l, r; Scan (l); Scan (r); Scan (x); if (x & 1) printf ("% d \ n", r-l + 1); else printf ("% d \ n ", sum [x> 1] [r]-sum [x> 1] [L-1]) ;}} return 0 ;}

 

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.