Hamster’s Sequence(前缀和 思维)

题目描述
WCQ is a very cute hamster.
He is interested in the number of factors of a number. One day his master WK gave him a sequence (a1, a2, …an) and m queries. For each query, WCQ will be given two integers l, r. He has to calculate the number of factors of .
As the cleverest hamster, he finds this problem very easy to handle,so he decides to ask you the same question.

输入
The first line contains two integers n and m (1 ≤ n, m ≤ 100000)—the sequence length and the number of questions.
The second line contains n integers a1, a2,…, an(1 ≤ ai ≤ 231 − 1)—the sequence.All the ai are generated by rand() ∗ rand(), where max { rand() } = 32767.
The next m line each line contains two integers l, r(1 ≤ l, r ≤ n), representing the queries.

输出
Print m integers ,ith of them is the answer to the ith query.
The answer should be mod 35808247, and 35808247=5981 × 5987.

样例输入
5 5
1 2 3 4 5
1 1
2 2
3 3
1 5
4 5

样例输出
1
2
2
16
6

思路
由于该序列的数都是由rand()函数构造而成,因此只需要通过求该序列质因数的个数,即可得到答案

代码实现

#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef pair<int,int>P;
const int N =1e5+5;
const int maxn=32767;
const ll mod = 35808247;
int cnt;
bool vis[N];
int prime[N];
 
void getprime()
{
    for(int i=2;i<=maxn;i++)
    {
        if(!vis[i]) prime[cnt++]=i;
        for(int j=0;j<cnt && i*prime[j]<=maxn;j++)
        {
            vis[i*prime[j]]=true;
            if(i%prime[j]==0) break;
        }
    }
}
ll ans[N],sum[N];
int a[N];
int n,m;
P ques[N];
void getq(int p)
{
    for(int i=1;i<=n;i++)
    {
        int temp=a[i];
        int tot=0;
        while(temp%prime[p]==0)
        {
            tot++;
            temp/=prime[p];
        }
        sum[i]=sum[i-1]+tot;
    }
    for(int i=1;i<=m;i++)
    {
        ll tmp=sum[ques[i].second]-sum[ques[i].first-1]+1;
        ans[i]=ans[i]*tmp%mod;
    }
}
int main()
{
    getprime();
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        ans[i]=1;
    }
    for(int i=1;i<=m;i++) scanf("%d%d",&ques[i].first,&ques[i].second);
    for(int i=0;i<cnt;i++) getq(i);
    for(int i=1;i<=m;i++) printf("%lld\n",ans[i]%mod);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来帮您解答这个问题。 首先,我们需要创建一个Rodent(啮齿动物)的抽象类,提供对所有Rodent通用的抽象方法。这个类如下所示: ```java public abstract class Rodent { public abstract String getType(); public abstract String getFoodType(); } ``` 注意,这个类是抽象的,因为我们不会直接创建Rodent对象,而是创建它的子类(Mouse、Mole、Hamster)的对象。 接下来,我们需要创建Mouse、Mole、Hamster的子类,并实现它们特定的行为习性。这些子类如下所示: ```java public class Mouse extends Rodent { public String getType() { return "Mouse"; } public String getFoodType() { return "Omnivore"; } } public class Mole extends Rodent { public String getType() { return "Mole"; } public String getFoodType() { return "Insectivore"; } } public class Hamster extends Rodent { public String getType() { return "Hamster"; } public String getFoodType() { return "Herbivore"; } } ``` 每个子类都实现了getType和getFoodType方法,用于获取子类的类型和主食类型。例如,Mouse的getType方法返回字符串"Mouse",getFoodType方法返回字符串"Omnivore",表示老鼠是杂食动物。 最后,我们需要编写主类,创建Mouse、Mole、Hamster的对象,并调用它们的方法输出相关信息。主类如下所示: ```java public class Main { public static void main(String[] args) { Rodent mouse = new Mouse(); Rodent mole = new Mole(); Rodent hamster = new Hamster(); System.out.println("Type: " + mouse.getType() + ", Food Type: " + mouse.getFoodType()); System.out.println("Type: " + mole.getType() + ", Food Type: " + mole.getFoodType()); System.out.println("Type: " + hamster.getType() + ", Food Type: " + hamster.getFoodType()); } } ``` 这个主类创建了三个Rodent对象,分别是Mouse、Mole、Hamster的实例。然后,它调用每个对象的getType和getFoodType方法,并输出相关信息。 当我们运行这个程序时,输出如下: ``` Type: Mouse, Food Type: Omnivore Type: Mole, Food Type: Insectivore Type: Hamster, Food Type: Herbivore ``` 这表明我们的代码已经成功输出了每个啮齿动物的类型和主食类型,符合题目要求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值