Hrbust2016年校团队赛E题

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);
        }

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值