A - 口算训练 (思维)

文章介绍了一种方法,通过质因子分解n个数并将质因子的位置存储,实现快速查询给定区间内某个数质因子的数量,使用二分查找优化查找过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门:nefu-10-15 - Virtual Judge (vjudge.net)

思路:

将n个数的质因子分解出来,将询问的数也质因子分解,看询问的数质因子数是否小于等于询问区间的质因子数,

问题关键在:怎样快速找到质因子在相应区间个数,我们可以存n个数每个数质因子出现的位置;

然后到询问一个质因子时,我们就到相应质因子,看l<=x<=r(x为质因子位置),有多少个(可以二分)

代码:

#define _CRT_SECURE_NO_WARNINGS 
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int N = 1e5 + 5;
int T, n, m, x;
vector<int> p[N];
int seek(int l, int r, int x)
{
    return upper_bound(p[x].begin(), p[x].end(), r) - lower_bound(p[x].begin(), p[x].end(), l);
}
int main() {
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d%d", &n, &m);
        for (int i = 0; i <= 1e5+5; i++)
            p[i].clear();
        for (int i = 1; i <= n; i++)
        {
            scanf("%d", &x);
            for (int j = 2; j <= x / j; j++)
            {
                while (x % j == 0)
                {
                    p[j].push_back(i);
                    x = x / j;
                }
            }
            if (x > 1) p[x].push_back(i);
        }
        int l, r, cnt = 0, cnt1, flag = 0;
        for (int i = 1; i <= m; i++)
        {
            scanf("%d%d%d", &l, &r, &x);
            if (x == 1)
            {
                printf("Yes\n");
                continue;
            }
            flag = 0;
            for (int j = 2; j <= x / j; j++)
            {
                cnt = 0;
                while (x % j == 0)
                    cnt++, x /= j;
                if (cnt > 0)
                {
                    cnt1 = seek(l, r, j);
                    if (cnt1 < cnt)
                    {
                        flag = 1;
                        break;
                    }
                }
            }
            if (x > 1)
            {
                cnt = seek(l, r, x);
                if (cnt < 1)
                    flag = 1;
            }
            if (flag)
                printf("No\n");
            else
                printf("Yes\n");
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值