链接: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 (li, 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