您的位置:首页 > 其它

CF Hello 2015 A ST表模板

2016-10-05 21:44 246 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: