Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)
Problem Description
小Q非常喜欢数学,但是他的口算能力非常弱。因此他找到了小T,给了小T一个长度为
n的正整数序列
a1,a2,...,an,要求小T抛出
m个问题以训练他的口算能力。
每个问题给出三个正整数 l,r,d,小Q需要通过口算快速判断 al×al+1×...×ar−1×ar是不是 d的倍数。
小Q迅速地回答了出来,但是小T并不知道正确答案是什么,请写一个程序帮助小T计算这些问题的正确答案。
每个问题给出三个正整数 l,r,d,小Q需要通过口算快速判断 al×al+1×...×ar−1×ar是不是 d的倍数。
小Q迅速地回答了出来,但是小T并不知道正确答案是什么,请写一个程序帮助小T计算这些问题的正确答案。
Input
第一行包含一个正整数
T(1≤T≤10),表示测试数据的组数。
每组数据第一行包含两个正整数 n,m(1≤n,m≤100000),分别表示序列长度以及问题个数。
第二行包含 n个正整数 a1,a2,...,an(1≤ai≤100000),表示序列中的每个数。
接下来 m行,每行三个正整数 l,r,d(1≤l≤r≤n,1≤d≤100000),表示每个问题。
每组数据第一行包含两个正整数 n,m(1≤n,m≤100000),分别表示序列长度以及问题个数。
第二行包含 n个正整数 a1,a2,...,an(1≤ai≤100000),表示序列中的每个数。
接下来 m行,每行三个正整数 l,r,d(1≤l≤r≤n,1≤d≤100000),表示每个问题。
Output
对于每个问题输出一行,若是倍数,输出Yes,否则输出No。
Sample Input
1 5 4 6 4 7 2 5 1 2 24 1 3 18 2 5 17 3 5 35
Sample Output
Yes No No Yes
Source
Recommend
思路:我们对输入的每一个数字分解质因数,分解过程中把下标存入对应的质因子中数组中。然后对于每次查询L,R,X,我们只需要对X分解质因数,然后统计当前这个质因数(假设为i)的个数cnt,如果cnt大于在L~R区间中 i 的个数那么就不行。那么怎么寻找在L~R区间中的 i 的个数呢。
开一个vector<int> G[MAXN]。G[i]存的是含有i这个因子的下标。
二分找到 R 的上界 和 L 的下界,相减即可。
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <utility>
#include <set>
#include <bitset>
#include <vector>
#define pi acos(-1.0)
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3fLL
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int MAXN = 100005;
int n, m;
vector<int> G[MAXN];//G[i]存的是含有i这个因子的下标
void work(int id, int x)
{
for (int i = 2; i*i <= x; i++)
{
while (x%i == 0)
{
G[i].push_back(id);
x /= i;
}
}
if (x > 1) G[x].push_back(id);//它本身即 它是质数
}
int query(int l, int r, int x)//L~R区间x的个数
{
return upper_bound(G[x].begin(), G[x].end(), r)//因为上取整表示找到r+1
- lower_bound(G[x].begin(), G[x].end(), l);
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
for (int i = 0; i < MAXN; i++) G[i].clear();
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
{
int t;
scanf("%d", &t);
work(i, t);//分解质因数并统计
}
while (m--)
{
int l, r, x; scanf("%d%d%d", &l, &r, &x);
bool flag = true;
for (int i = 2; i*i <= x; i++)
{
int cnt = 0;
while (x%i == 0)
{
cnt++;
x /= i;
}
if (cnt > query(l, r, i)) { flag = false; break; }
}
if (flag && x > 1 && query(l, r, x) == 0)//剩下的除不掉的在(l,r)区间里找看是否存在
flag = false;
if (flag)
printf("Yes\n");
else
printf("No\n");
}
}
return 0;
}
/*
2
5 4
6 4 7 2 5
1 2 24
1 3 18
2 5 17
3 5 35
3 2
634 5 7
1 1 317
2 3 317
*/