• CodeForces


    Yash is finally tired of computing the length of the longest Fibonacci-ish sequence. He now plays around with more complex things such as Fibonacci-ish potentials.

    Fibonacci-ish potential of an array ai is computed as follows:

    1. Remove all elements j if there exists i < j such that ai = aj.
    2. Sort the remaining elements in ascending order, i.e. a1 < a2 < ... < an.
    3. Compute the potential as P(a) = a1·F1 + a2·F2 + ... + an·Fn, where Fi is the i-th Fibonacci number (see notes for clarification).

    You are given an array ai of length n and q ranges from lj to rj. For each range j you have to compute the Fibonacci-ish potential of the array bi, composed using all elements of ai from lj to rj inclusive. Find these potentials modulo m.

    Input

    The first line of the input contains integers of n and m (1 ≤ n, m ≤ 30 000) — the length of the initial array and the modulo, respectively.

    The next line contains n integers ai (0 ≤ ai ≤ 109) — elements of the array.

    Then follow the number of ranges q (1 ≤ q ≤ 30 000).

    Last q lines contain pairs of indices li and ri (1 ≤ li ≤ ri ≤ n) — ranges to compute Fibonacci-ish potentials.

    Output

    Print q lines, i-th of them must contain the Fibonacci-ish potential of the i-th range modulo m.

    Example

    Input
    5 10
    2 1 2 1 2
    2
    2 4
    4 5
    Output
    3
    3

    题意:Q次询问,每次询问给一个区间,让你把区间排序,按顺序乘对应的斐波拉契数。

    思路: 正解占位,暂时不会。 只把暴力的码了。 暴力:每次看每个数是否存在于当前区间,然后做乘即可。复杂度O(nq)。

    #include<bits/stdc++.h>
    #define rep(i,a,b) for(int i=a;i<=b;i++)
    #define pii pair<int,int>
    #define F first
    #define S second
    using namespace std;
    const int maxn=30010;
    pii a[maxn]; int f[maxn],L[maxn],R[maxn],num[maxn],ans[maxn],lt[maxn];
    int main()
    {
        int N,M,P;
        scanf("%d%d",&N,&P);
        f[1]=1; f[2]=1; rep(i,3,N) f[i]=(f[i-1]+f[i-2])%P;
        rep(i,1,N) scanf("%d",&a[i].F),a[i].F,a[i].S=i;
        sort(a+1,a+N+1);
        scanf("%d",&M);
        rep(i,1,M) {
            scanf("%d%d",&L[i],&R[i]);
            lt[i]=-1;
        }
        rep(i,1,N){
            int d=a[i].F%P;
            rep(j,1,M){
                if(a[i].S>R[j]||a[i].S<L[j]) continue;//
                if(a[i].F==lt[j]) continue;
                ans[j]=(ans[j]+f[++num[j]]*d)%P;
                lt[j]=a[i].F;
            }
        }
        rep(i,1,M) printf("%d
    ",ans[i]);
        return 0;
    }
  • 相关阅读:
    六种常见排序算法的自我研究(冒泡排序,选择排序,快速排序,归并排序,插入排序,堆排序)
    设计模式学习总结(二)工厂模式
    SQL Server Reporting Service(SSRS) 第七篇 常见错误汇总
    SQL Server Reporting Service(SSRS) 第六篇 SSRS 部署总结
    设计模式学习总结(一)简单工厂模式
    设计模式学习总结(三)抽象工厂模式
    设计模式学习总结(四)单例模式
    SQL Server覆盖索引--有无包含列对数据库查询性能的影响分析
    Dev Express Report 学习总结(八)Dev Express Reports 常见问题总结
    docker命令笔记
  • 原文地址:https://www.cnblogs.com/hua-dong/p/9676012.html
Copyright © 2020-2023  润新知