Mod
Time Limit: 1000 MS Memory Limit: 100000 K
Total Submit: 28(5 users) Total Accepted: 8(4 users) Rating: Special Judge: No
Description
Kim刚刚学会C语言中的取模运算(mod)。他想要研究一下一个数字A模上一系列数后的结果是多少。帮他写个程序验证一下。
Input
第一行一个整数T代表数据组数。
接下来T组数据,第一行一个整数n,接下来n个数字ai
接下来一行一个整数m,接下来m个数字bi。
Output
对于每个bi,输出bi%a1%a2%…%an 。
Sample Input
1
4
10 9 5 7
5
14 8 27 11 25
Sample Output
4
3
2
1
0
Hint
在C语言中,A mod B 是 a%b
样例解释:
14%10%9%5%7=4
8%10%9%5%7=3
…
数据范围:
1<=n<=100000
1<=m<=100000
1<=ai<=1000000000
0<=bi<=1000000000
Source
“科林明伦杯”哈尔滨理工大学第六届程序设计团队赛
题解:线段树维护即可,跟HDU5875做法相同,不再说明。
代码:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <algorithm>
#define LL long long
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
const int N=100002;
LL sum[N<<2];
LL a[N];
int x,y,n,m,t;
LL ans;
void pushup(int rt)
{
sum[rt]=min(sum[rt<<1],sum[rt<<1|1]);
}
void build(int l,int r,int rt)
{
if(l==r)
{
scanf("%d",&sum[rt]);
a[l]=sum[rt];
}
else
{
int mid=(r+l)>>1;
build(lson);
build(rson);
pushup(rt);
}
}
int query(int l,int r,int ll,int rr,int rt,int k)
{
if(sum[rt]>k)
return rr+1;
if(ll<=l&&rr>=r)
{
if(l==r)
{
return l;
}
int mid=(r+l)>>1;
if(sum[rt<<1]<=k)
return query(l,mid,ll,rr,rt<<1,k);
else
return query(mid+1,r,ll,rr,rt<<1|1,k);
}
else
{
int mid=(r+l)>>1,res;
if(ll<=mid)
{
res=query(l,mid,ll,rr,rt<<1,k);
if(res<=rr)
return res;
}
if(rr>mid)
{
res=query(mid+1,r,ll,rr,rt<<1|1,k);
if(res<=rr)
return res;
}
}
return rr+1;
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
build(1,n,1);
scanf("%d",&m);
while(m--)
{
scanf("%lld",&ans);
x=1;
while(1)
{
int q=query(1,n,x,n,1,ans);
if(q<=n)
ans%=a[q];
else break;
x=q+1;
}
printf("%lld\n",ans);
}
}
}