codeforces 475D. CGCDSSQ

针对大量区间GCD查询问题,采用预处理与离线查询优化算法。通过建立前缀树和利用并查集技巧,实现高效查询区间内整数的最大公约数等于特定值的子区间的数量。

D. CGCDSSQ

                                  time limit per test 2 seconds

memory limit per test 256 megabytes

Given a sequence of integers a1, ..., an and q queries x1, ..., xq on it. For each query xi you have to count the number of pairs (l, r)such that 1 ≤ l ≤ r ≤ n and gcd(al, al + 1, ..., ar) = xi.

 is a greatest common divisor of v1, v2, ..., vn, that is equal to a largest positive integer that divides all vi.

Input

The first line of the input contains integer n, (1 ≤ n ≤ 105), denoting the length of the sequence. The next line contains n space separated integers a1, ..., an, (1 ≤ ai ≤ 109).

The third line of the input contains integer q, (1 ≤ q ≤ 3 × 105), denoting the number of queries. Then follows q lines, each contain an integer xi, (1 ≤ xi ≤ 109).

Output

For each query print the result in a separate line.

Examples

input

3
2 6 3
5
1
2
3
4
6

output

1
2
2
0
1

input

7
10 20 3 15 1000 60 16
10
1
2
3
4
5
6
10
20
60
1000

output

14
0
2
2
2
0
2
2
1
1

题目大意:

         一个长度为n的a数列,q次询问。每次询问一个数值x,求解有多少个[l,r](1<=l<=r<=n)满足Gcd(a[l],a[l+1],……,a[r])为x。

         其中n<=1e5,q<=3e5,任意x,a[i]满足1<=x,a[i]<=1e9;

题解:

         显然,对于每个询问我们都不得不枚举每个左端点,然后查询满足条件的右端点区间,然而在线超时,所以我们可以把询问用map记录,离线处理。又数组为静态,我们只需要静态维护一下区间最大公约数。同时把统计得到的每个询问结果累加即可。

#include<cstdio>
#include<map>
typedef long long ll;
const int N=(int)1e6+10;
inline void read(int &x){
    x=0;char ch=getchar();
    while(ch<'0'||ch>'9')   ch=getchar();
    while(!(ch<'0'||ch>'9'))  x=x*10+ch-48,ch=getchar();
}
int n,m;
int gcd(int x,int y){return y==0?x:gcd(y,x%y);}
std::map<int ,ll > query;
int a[N],lg[N],bin[20];
int f[20][N];
inline int ques(int l,int r){
    if(r==n+1)  return 0;
    int t=lg[r-l+1];
    return gcd(f[t][l],f[t][r-bin[t]+1]);
}
inline void init(){
    lg[0]=-1;for(int i=1;i<=n;i++)lg[i]=lg[i>>1]+1;
    bin[0]=1;for(int i=1;i<=18;i++)  bin[i]=bin[i-1]<<1;
    for(int i=1;i<=n;i++)   f[0][i]=a[i];
    for(int i=1;i<=lg[n];i++)
        for(int j=1;j+bin[i]<=n+1;j++){
            f[i][j]=gcd(f[i-1][j],f[i-1][j+bin[i-1]]);
        }
}
inline  int find(int x,int l,int op){
    int r=n+1;
    while(l<r-1){
        int mid=l+r>>1;
        if(ques(op,mid)!=x) r=mid;
        else l=mid;
    }
    return l;
}
inline void solve(int x){
    int t=a[x],now=x;
    int last;
    while(now!=n+1){
        last=now;
        now=find(t,now,x);
        if(query[t])query[t]+=now-last+1;
        now++;t=ques(x,now);
    }
}
int x[N];
int main(){
    read(n);
    for(int i=1;i<=n;i++)   read(a[i]);
    read(m);
    init();
    for(int i=1;i<=m;i++){
        read(x[i]);
        query[x[i]]=1;
    }
    for(int i=1;i<=n;i++)solve(i);
    for(int i=1;i<=m;i++)
        printf("%I64d\n",query[x[i]]-1);
  //while(1);
}

 

转载于:https://www.cnblogs.com/Troywar/p/7233270.html

在车辆工程中,悬架系统的性能评估和优化一直是研究的热点。悬架不仅关乎车辆的乘坐舒适性,还直接影响到车辆的操控性和稳定性。为了深入理解悬架的动态行为,研究人员经常使用“二自由度悬架模型”来简化分析,并运用“传递函数”这一数学工具来描述悬架系统的动态特性。 二自由度悬架模型将复杂的车辆系统简化为两个独立的部分:车轮和车身。这种简化模型能够较准确地模拟出车辆在垂直方向上的运动行为,同时忽略了侧向和纵向的动态影响,这使得工程师能够更加专注于分析与优化与垂直动态相关的性能指标。 传递函数作为控制系统理论中的一种工具,能够描述系统输入和输出之间的关系。在悬架系统中,传递函数特别重要,因为它能够反映出路面不平度如何被悬架系统转化为车内乘员感受到的振动。通过传递函数,我们可以得到一个频率域上的表达式,从中分析出悬架系统的关键动态特性,如系统的振幅衰减特性和共振频率等。 在实际应用中,工程师通过使用MATLAB这类数学软件,建立双质量悬架的数学模型。模型中的参数包括车轮质量、车身质量、弹簧刚度以及阻尼系数等。通过编程求解,工程师可以得到悬架系统的传递函数,并据此绘制出传递函数曲线。这为评估悬架性能提供了一个直观的工具,使工程师能够了解悬架在不同频率激励下的响应情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值