CF Hello 2015 A ST表模板

A. LCM Query
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
De Prezer loves lcm (Least Common Multiple).Ha has got a sequence a1, a2, …, an but doesn’t know how to calculate lcm of two numbers.

De Prezer also loves query.So he asks you to answer to m queries on this sequence.

In each query, he gives you number x and you should print the following number :

lcm(ai, ai + 1, …, ai + x - 1)

Answer can be very large, so print it modulo 109 + 7 .

Input
The first line of input consists of 2 integers n and m.

The second line of input contains n space separated integers a1, a2, …, an.

The next m lines, each line contains an integer x.

1 ≤ n ≤ 2 * 104

1 ≤ m ≤ 106

1 ≤ ai ≤ 60 (For each 1 ≤ i ≤ n)

1 ≤ x ≤ n

Output
Print m lines, each answer to one query.

Examples
input
5 5
1 2 3 4 5
1
2
3
4
5
output
1
2
6
12
60
input
5 5
2 3 1 4 5
1
2
3
4
5
output
1
3
6
12
60

题意:长度为20000的数列(ai<=60),处理1e6次询问,求长度为k的子序列最小的lcm。

做法:ST表预处理。枚举左端点,二分右端点。这里lcm会爆ll,用60以内17个质数的次数存。判断大小取对数就可以了。每次二分到一个拐点,将该长度更新一次。因为最小lcm关于长度是递增的,最后对ans数组做一下单调处理就好了。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1000000007;

int n, m;
int fuckref[18]={0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59};
double lref[30];

void init()
{
    for(int i=1;i<=17;i++)
        lref[i]=log(fuckref[i]);
}

struct node{
    int tim[20];
};

node f[25000][20];
node ans[25000];

bool operator<(const node& a, const node& b)
{
    double ta=0, tb=0;
    for(int i=1;i<=17;i++)
    {
        ta+=a.tim[i]*lref[i];
        tb+=b.tim[i]*lref[i];
    }
    return ta<tb;
}

bool operator==(const node&a, const node&b)
{
    for(int i=1;i<=17;i++)if(a.tim[i]!=b.tim[i])return false;
    return true;
}

node lcm(node x,node y)
{
    node ans;
    for(int i=1;i<=17;i++){
        ans.tim[i]=max(x.tim[i], y.tim[i]);
    }
    return ans;
}

node getlcm(int l,int r)
{
    int j=31-__builtin_clz(r-l+1);
    return lcm(f[l][j],f[r-(1<<j)+1][j]);
}

int main()
{
    init();
    while(~scanf("%d%d",&n, &m))
    {
        for(int i=1;i<=n;i++)
            for(int j=1;j<=17;j++)
            {
                ans[i].tim[j]=10;
            }
        for(int i=0;i<n;i++){
            int tmp;
            scanf("%d", &tmp);
            for(int j=1;j<=17;j++)
            {
                f[i][0].tim[j]=0;
                while(tmp%fuckref[j]==0){
                    tmp/=fuckref[j];
                    f[i][0].tim[j]++;
                }
            }
        }
        for(int j=1;j<=19;j++)
        {
            for(int i=0;i<n;i++)
            {
                if(i+(1<<j)-1<n)
                {
                    f[i][j]=lcm(f[i][j-1],f[i+(1<<(j-1))][j-1]);
                }
                else{
                    break;
                }
            }
        }
        for(int i=0;i<n;i++)
        {
            int ed=i;
            while(ed<n)
            {
                node g=getlcm(i,ed);
                int l=ed,r=n-1;
                int m,tag=-1;
                while(l<=r)
                {
                    if(r-l<=1)
                    {
                        if(getlcm(i,r)==g)tag=r;
                        else tag=l;
                        break;
                    }
                    m=(l+r)>>1;
                    if(getlcm(i,m)==g)l=m;
                    else r=m;
                }
                if(g<ans[tag-i+1]){ans[tag-i+1]=g;}
                ed=tag+1;
            }
        }
        for(int i=n-1;i>=1;i--)
        {
            if(ans[i+1]<ans[i])ans[i]=ans[i+1];
        }
        while(m--)
        {
            int tmp;
            scanf("%d",&tmp);
            ll top=1;
            node ansn=ans[tmp];
            for(int i=1;i<=17;i++)
                while(ansn.tim[i]){
                    top=(top*fuckref[i])%mod;
                    ansn.tim[i]--;
                }
            printf("%lld\n",top);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值