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
直接写的话会超时
所以可以先判断数列中的每个数的质因数的个数,在分解d判断d的质因数的区间[l,r]中的质因数的个数多少
#include <iostream> #include<math.h> #include<bits/stdc++.h> #include<vector> using namespace std; #define ll long long const int maxn=1e5+5; const int mod=1e5; vector<int> a[maxn];// int query(int l , int r , int x){ return upper_bound( a[x].begin() ,a[x].end() , r) -lower_bound( a[x].begin() , a[x].end() , l);//因为是按顺序记录的i,所以可以找【l,r】这个区间里x的个数 } int solve(int l , int r , int d) { for(int i = 2 ; i * i <= d ; i++ ){ if ( d % i == 0 ) { int cnt = 0 ; while (d % i == 0 ) { cnt++ ; d /= i ; } if (cnt > query(l, r, i)) return 0;//如果i的个数小于应该有的个数,那么就不能整除d,返回0 } } if (d > 1) { if (query(l, r, d) < 1) return 0;//同上,但是d是被除后,所剩下的质数,如果【l,r】区间里没有d那么就返回0 } return 1; } int main() { int T; scanf("%d",&T); while(T--){ int n,m;scanf("%d %d",&n,&m); for (int i = 0; i < maxn; i++) a[i].clear(); for (int i = 1, x; i <= n; i++){ scanf("%d", &x); for (int j = 2; j * j <= x; j++){ while (x % j == 0){ x/=j; a[j].push_back(i);//i这个数可以分解成j,用j记录i } } if (x > 1) a[x].push_back(i); } while (m--) { int l,r,d; scanf("%d %d %d", &l, &r, &d); if(solve(l, r, d)) printf("Yes\n"); else printf("No\n"); } } return 0; }