1. 程式人生 > >某叫不出名字的冬令營J(離線+分治+揹包DP)

某叫不出名字的冬令營J(離線+分治+揹包DP)

連結:http://codeforces.com/gym/101741/problem/J

題意:給定一個序列,q次詢問,每次詢問從[l,r]從選若干個數使得這些數能被m整除的方案數

這個一開始就想到線段樹+揹包合併,複雜度O(nm^2logn),算了下好像很極限。。寫了下超了7倍時限。。線段樹常數還是太大了TAT

然後正解是分治,由於揹包合併的代價太大,我們更傾向於選擇一個一個元素往上加,因此對於一個分治點mid,如果區間跨過這個點,那麼可以先預處理出從mid向左延伸各個長度的揹包,向右延伸各個長度的揹包。。然後區間就能分成2段合併,由於只需要找出餘數為0的方案數,因此只需要O(m)列舉就可以了。。

每層分治預處理要O(n),每個線段合併要O(m),因此複雜度為O(nlogn+qm)

如果把長度比較小的線段預先暴力處理一下或許會更快?

 

 

/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神獸保佑,程式碼無bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1LL<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define NM 200015
#define nm 200005
#define pi 3.1415926535897931
using namespace std;
const ll inf=1e9+7;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}






struct tmp{int x,y,i;};
vector<tmp>d[NM];
int n,m,_x,_y,a[NM],p;
ll l[NM][20],r[NM][20],ans[NM];




void div(int i,int x,int y){
    int t=x+y>>1,tot=d[i].size()-1;
    if(y-x+1<=50||tot==-1)return;
    inc(j,t+1,y){
	inc(k,0,m-1)r[j-t][(k+a[j])%m]=r[j-t-1][k];
	inc(k,0,m-1)r[j-t][k]+=r[j-t-1][k],r[j-t][k]%=inf;
    }
    dec(j,t,x){
	inc(k,0,m-1)l[t-j+1][(k+a[j])%m]=l[t-j][k];
	inc(k,0,m-1)l[t-j+1][k]+=l[t-j][k],l[t-j+1][k]%=inf;
    }
    inc(j,0,tot)if(d[i][j].y<t)d[i<<1].push_back(d[i][j]);
    else if(d[i][j].x>t+1)d[i<<1|1].push_back(d[i][j]);
    else inc(k,0,m-1)
	ans[d[i][j].i]+=r[d[i][j].y-t][k]*l[t-d[i][j].x+1][(m-k)%m]%inf,ans[d[i][j].i]%=inf;
    d[i].clear();
    div(i<<1,x,t-1);div(i<<1|1,t+2,y);
}


int main(){
    //freopen("data.in","r",stdin);
    n=read();m=read();
    inc(i,1,n)a[i]=read()%m;
    l[0][0]=r[0][0]=1;
    p=read();
    inc(i,1,p){
	_x=read();_y=read();
	if(_y-_x+1>50)d[1].push_back(tmp{_x,_y,i});
	else{
	    inc(i,_x,_y){
		inc(j,0,m-1)l[i-_x+1][(j+a[i])%m]=l[i-_x][j];
		inc(j,0,m-1)l[i-_x+1][j]+=l[i-_x][j],l[i-_x+1][j]%=inf;
	    }
	    ans[i]=l[_y-_x+1][0];
	}
    }
    div(1,1,n);
    inc(i,1,p)printf("%lld\n",ans[i]);
    return 0;
}

 

 

 

J. Subsequence Sum Queries

time limit per test

2 seconds

memory limit per test

256 mebibytes

input

standard input

output

standard output

You have an array a containing n integers and an integer m. You also have q queries to answer. The i-th query is described as a pair of integers (l

i, ri). Your task is to calculate the number of such subsequences aj1, aj2, ..., ajk that li ≤ j1 < j2 < ... < jk ≤ ri and . In other words, you need to calculate the number of subsequences of subarray [ali, ali + 1, ..., ari] such that the sum of elements in each subsequence is divisible by m.

Input

The first line contains two integers n and m: the number of elements in a and the modulo (1 ≤ n ≤ 2·105, 1 ≤ m ≤ 20).

The second line contains n integers ai: the elements of array a (0 ≤ ai ≤ 109).

The third line contains one integer q: the number of queries (1 ≤ q ≤ 2·105).

Then q lines follow. The i-th of these lines contains two integers li and ri that describe the i-th query (1 ≤ li ≤ ri ≤ n).

Output

Print q lines. The i-th of them must contain the answer for the i-th query. Queries are indexed in the order they are given in the input. Since the answers can be very large, print them modulo 109 + 7.

Example

Input

Copy

4 3
5 1 3 2
4
1 2
1 3
1 4
2 4

Output

Copy

2
4
6
4