zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    仿酷狗音乐播放器开发日志二十七 用ole为窗体增加文件拖动功能(附源码)
    redis持久化和主从同步
    MySQL主从复制
    Nginx 安装与详解
    ContOS安装配置MySQL,redis
    ContOS7编译安装python3,配置虚拟环境
    ContOS7切换国内源
    ContOS 常用命令
    轮询、长轮询、websock
    flask之三方组件
  • 原文地址:https://www.cnblogs.com/hua-dong/p/9676012.html
Copyright © 2011-2022 走看看