题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3940
题目大意:对于0到m区间的数模Ai,模A2...模An,later有q个询问,问前面的问题等于yi的数有多少个(1<=i<=q)最后在按照题意求一下最终结果。
解题思路:做的时候最关键的部分没有想明白,没有找到好的处理方法,还是经验少吧,题解中关键的两点:一个区间模一个数还是一个或几个区间,因为区间的左端点都是0,所以用map存区间和区间的个数,模i的时候只需要模区间右端点值大于等于i的,模完后取一下后缀和,对于每次询问用二分查找。lower_bound()在这很好用。
/* ********************************
Author : danmu
Created Time : 2016年04月23日 星期六 13时50分09秒
File Name : e.cpp
Vim Command
copy -> yy
paste -> p P
del this line -> dd
Ctrl-Z -> u U
copy into system -> "+y
******************************** */
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <string>
#include <vector>
#include <cstdio>
#include <stack>
#include <queue>
#include <cmath>
#include <list>
#include <map>
#include <set>
#define ULL unsigned long long
#define PI 3.1415926535
#define INF 0x3f3f3f3f
#define LL long long
#define eps 1e-8
#define mod 1000000007
using namespace std;
int a[100010],cnt;
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int t;
scanf("%d",&t);
while(t--){
int n,m,q,ans=0,y;
cnt=0;
scanf("%d%d",&n,&m);
for(int i=0;i<n;++i)
{
scanf("%d",a+cnt);
if(cnt==0&&a[cnt]<=m){
++cnt;
continue;
}
if(cnt&&a[cnt]<a[cnt-1])
++cnt;
}
map<int,int> num;
num[m]=1;
for(int i=0;i<cnt;++i){
for(map<int,int>::iterator j=num.lower_bound(a[i]);j!=num.end();j++){
if(num.find(a[i]-1)==num.end())
num[a[i]-1]=(j->first+1)/a[i]*j->second;
else
num[a[i]-1]+=(j->first+1)/a[i]*j->second;
if((j->first+1)%a[i]){
if(num.find((j->first+1)%a[i]-1)==num.end())
num[(j->first+1)%a[i]-1]=j->second;
else
num[(j->first+1)%a[i]-1]+=j->second;
}
}
map<int,int>::iterator tmp=num.end();
while((--tmp)->first>=a[i]){
num.erase(tmp);
tmp=num.end();
}
}
int sum=0;
map<int,int>::reverse_iterator i=num.rbegin();
for(;i!=num.rend();++i){
i->second=(i->second+sum)%mod;
sum=i->second;
}
scanf("%d",&q);
for(int i=1;i<=q;++i){
scanf("%d",&y);
map<int,int>::iterator tmp=num.lower_bound(y);
ans=(ans+((LL)i*(tmp->second)))%mod;
}
printf("%d\n",ans);
}
return 0;
}