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=;
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[]=; f[]=; rep(i,,N) f[i]=(f[i-]+f[i-])%P;
rep(i,,N) scanf("%d",&a[i].F),a[i].F,a[i].S=i;
sort(a+,a+N+);
scanf("%d",&M);
rep(i,,M) {
scanf("%d%d",&L[i],&R[i]);
lt[i]=-;
}
rep(i,,N){
int d=a[i].F%P;
rep(j,,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,,M) printf("%d\n",ans[i]);
return ;
}

最新文章

  1. String类的功能
  2. Linux中的输入重定向,变量
  3. WinForm支持拖拽效果
  4. Silverlight独立存储
  5. 【程序员的SQL金典】笔记(第6章~第11章)
  6. ORA-15025: could not open disk 处理
  7. hdu 2767 Proving Equivalences
  8. Delphi Register
  9. 设计模式 - 出厂模式(factory pattern) 详细说明
  10. 字段的参数 -- Django从入门到精通系列教程
  11. C语言中return 0和return 1和return -1
  12. LimeSDR 上手指南
  13. Xgboost: 一把屠龙刀的自我修养
  14. C#代码安装Windows服务
  15. ubuntu安装anaconda后,终端输入conda,出现未找到命令
  16. Bootstrap(6)辅组类和响应式工具
  17. libgdx学习记录23——图片移动选择
  18. Oracle学习笔记:a inner join b与from a,b where a.x=b.x的差异
  19. SVG渲染顺序及z轴显示问题(zIndex)
  20. squid搭建http/https代理服务器

热门文章

  1. jQuery:自学笔记(2)——jQuery选择器
  2. LVM逻辑磁盘管理
  3. MFC输出调试信息
  4. $Android AlertDialog的各种用法总结
  5. oracle 定时删除3天前的备份数据
  6. RTC是DS1339,驱动采用的是rtc-ds1307.c
  7. 跨平台移动开发_Windows Phone 8 使用 PhoneGap 方法
  8. 20145230《Java程序设计》第5周学习总结
  9. Python 字符串概念和操作
  10. Python日期时间函数