Gym 101102J---Divisible Numbers(反推技巧题)

题目链接

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

Description

standard input/output

You are given an array A of integers of size N, and Q queries. For each query, you will be given a set of distinct integers S and two integers L and R that 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 integer T, the number of test cases.

The first line of each test case contains two integers, N and Q (1 ≤ N, Q ≤ 105), the size of the array and the number of queries, respectively.

The next line contains N space-separated integers, the values of the array A (1 ≤ Ai ≤ 109).

Each of the next Q lines contain the description of one query in the form:

LRS

Where L and R (1 ≤ L ≤ R ≤ N) represent the range, and S is an integer between 1 and 1023 (inclusive) and represents the set; consider the binary representation of the number S, if the ith bit (1-based) is 1, then the number i belongs to the set. Since S is less than1024, the values in the set are between 1 and 10.

For example: if S is 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

14 22 5 3 81 3 22 4 355

Output

13

题意:输入n,q表示由n个数构成的一个序列,q次询问,每次询问输入l r s s表示一个集合,s的二进制中的1的位置,如:6(10)=110(2) 对应集合{2,3} 现在求区间l~r中能被集合中的(任意一个)数整除的数的个数;

思路:因为s取值范围为1~1023 所以输入n个数时直接求出能被哪些s(对应的集合数)整除,然后用前缀和表示出来,那么每次查询时就是0(1)的了;

代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
typedef long 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));  加上超时!!!
        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;
}
时间: 2024-08-08 13:50:49

Gym 101102J---Divisible Numbers(反推技巧题)的相关文章

反调试技巧总结-原理和实现

标 题: [原创]反调试技巧总结-原理和实现(1)(2)(3)(4)(5)(6)......作 者: shellwolf时 间: 2008-08-10,22:40链 接: http://bbs.pediy.com/showthread.php?t=70470 反调试技巧总结-原理和实现-------------------------------------------------------------------------------------------------------2008

从YouTube算法论文反推其推荐机制

去年,在波士顿举办的第10届ACM推荐系统大会(ACM's RecSys '16)上,来自Google的一个研究团队公布了YouTube推荐系统的深度学习论文:Deep Neural Networks for YouTube Recommendations 论文作者是Google的软件工程师 Jay Adams 与高级软件工程师 Paul Covington.Embre Sargin,他们向业界展示了YouTube在机器学习推荐算法上的深度神经网络使用情况.该论文涉及到一些非常技术.非常高级的细

base64随机字符混淆加密、解密-美拍视频地址解密,反推加密算法

用火车头测试采集美拍的数据时无意中发现美拍的视频地址是一段加了混淆字符串的base64代码.如下图 于是好奇之下研究了下解密算法.具体过程省略800字.发现美拍的视频解密是通过js完成,于是找到了具体的解密代码,如下: 1 ;(function(a) { 2 var b = "substring", 3 c = "split", 4 d = "replace", 5 e = "substr", 6 f = { 7 getHex

反调试技巧

一. 前言 部分代码和参考资料来源:1.<<脱壳的艺术>> hawking2.<<windows anti-debugger reference>> Angeljyt3.http://bbs.pediy.com 4.<<软件加密技术内幕>> 看雪学院5.<<ANTI-UNPACKER TRICKS>> Peter Ferrie 二.反调试函数前缀              检测        攻击通用调试器  

Anipang2反推文档

此文档主要用于一个开发同学尝试学习描述一个产品的基本设计.也许工程师都应该有类似能力. 反推的基础,目前是自己玩过的一些关卡和youtube上的一些关卡通关视频,主要是前120关.(120关后面应该是最新版本才出). 1 消除块,棋子 颜色:青,黄,蓝,粉,褐,灰,6种,表现为小动物. 有些时候单局内只出现5种颜色的棋子. 如果消除的(N > 3)个棋子,棋盘会出现帽子(横,竖,球3种)棋子,晕眼棋子.如下图:中间有各种颜色的棋子,而且有2个晕眼棋子,横帽棋子,球帽棋子. 其他棋盘中的棋子还包括

先序中序反推二叉树

先序 :  A B D G H C E F i 中序 :  G D H B A E C i F 反推思路 :  先通过先序确定根,再根据中序确定左子右子  然后再通过先序确定根  再更新中序确定左子右子 反复推论即可 原文地址:https://www.cnblogs.com/weishao-lsv/p/8151028.html

组合电路(反推逻辑表达式,组合电路的控制结果只和输入变量的状态有关)

如何根据真值表反推逻辑表达式? 第一种方法:以真值表内输出端“1”为准 第一步:从真值表内找输出端为“1”的各行,把每行的输入变量写成乘积形式;遇到“0”的输入变量上加非号. 第二步:把各乘积项相加,即得逻辑函数的表达式. 第二种方法:以真值表内输出端“0”为准 第一步:从真值表内找输出端为“0”的各行,把每行的输入变量写成求和的形式,遇到“1”的输入变量上加非号. 第二步:把各求和项相乘,即得逻辑函数表达式. 最后化简,在实际运用过程中,哪个方法简便就采用哪种. 具体实例演示: 假设在楼梯走廊

codeforces Gym 100187L L. Ministry of Truth 水题

L. Ministry of Truth Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/K Description Andrey works in the Ministry of Truth. His work is changing articles in newspapers and magazines so that they praise the Party an

[C++]LeetCode: 108 Add Two Numbers (反序链表求和)

题目: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -&