ZOJ 3940 Modulo Query

0--M对某个数字取模,相当于把0--M区间进行切割,每次暴力切割一下。结果的算的时候二分一下即可。。。

看了官方题解才会。。。

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

const int maxn=100000+10;
long long mod=1e9+7;
int T,m,sz;
struct X
{
    int x;
    long long y;
    bool operator<(const X&a)const
    {
        return x < a.x;
    }
    X(int a,long long b) {x=a,y=b;}
};
long long sum[maxn],cnt[maxn];
int v[maxn];

long long get(int now)
{
    long long res=0;
    int l=0,r=sz-1;
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(v[mid]>=now)
        {
            l=mid+1;
            res=sum[mid];
        }
        else r=mid-1;
    }
    return res;
}

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        int n; scanf("%d%d",&n,&m);
        priority_queue<X>q;
        q.push(X(m,1));
        for(int i=1;i<=n;i++)
        {
            int x; scanf("%d",&x);
            while(1)
            {
                if(q.top().x<x) break;
                X head=q.top(); q.pop();
                q.push(X(x-1, (head.x + 1) / x*head.y));
                if ((head.x+1)%x>=1) q.push(X(head.x%x, head.y));
            }
        }

        sz=0;
        v[sz]=q.top().x; cnt[sz]=q.top().y;
        q.pop(); sz++;

        while(!q.empty())
        {
            X head=q.top(); q.pop();
            if(head.x==v[sz-1]) cnt[sz-1]+=head.y;
            else
            {
                v[sz]=head.x;
                cnt[sz]=head.y;
                sz++;
            }
        }

        sum[0]=cnt[0];
        for(int i=1;i<sz;i++) sum[i]=sum[i-1]+cnt[i];

        int Q; scanf("%d",&Q);
        long long ans=0;
        for(long long i=1;i<=Q;i++)
        {
            int g; scanf("%d",&g);
            ans=(ans+i*get(g)%mod)%mod;
        }

        printf("%lld\n",ans%mod);
    }
    return 0;
}
时间: 2024-10-11 16:07:47

ZOJ 3940 Modulo Query的相关文章

ZOJ 3911 Prime Query(线段树)

Prime Query Time Limit: 1 Second      Memory Limit: 196608 KB You are given a simple task. Given a sequence A[i] with N numbers. You have to perform Q operations on the given sequence. Here are the operations: A v l, add the value v to element with i

ZOJ 3911 Prime Query ZOJ Monthly, October 2015 - I

Prime Query Time Limit: 1 Second      Memory Limit: 196608 KB You are given a simple task. Given a sequence A[i] with N numbers. You have to perform Q operations on the given sequence. Here are the operations: A v l, add the value v to element with i

2016.4.23 浙江省赛题解

Apples and Ideas Time Limit: 2 Seconds      Memory Limit: 65536 KB "If you have an apple and I have an apple and we exchange these apples then you and I will still each have one apple. But if you have an idea and I have an idea and we exchange these

2016.4.23浙江省赛

A      Apples and Ideas Time Limit: 2 Seconds      Memory Limit: 65536 KB "If you have an apple and I have an apple and we exchange these apples then you and I will still each have one apple. But if you have an idea and I have an idea and we exchange

ZOJ 2015 10月份 月赛 3911 Prime Query

这道题我改啊,改啊,交啊,就对了. #include <stdio.h> #include <stdlib.h> #include <math.h> #include<cstring> #include<algorithm> using namespace std; const int N = 10000005; const int M = 100005; int a[N]; int p[M]; int tree[M*4],cover[M*4];

Raising Modulo Numbers(ZOJ 2150)

这是题目的答案 #include<iostream> #include<cmath> using namespace std; int a[45002]; int b[45002]; int mod(int a, int b, int c) { int z = 1; while(b) { if(b%2) z = (z*a)%c; b/=2; a = (a*a)%c; } return z; } int main() { int T; cin>>T; while(T--)

zoj 1610 Count the Colors 【区间覆盖 求染色段】

Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones. Your task is counting the segments of different colors you can s

ZOJ 3891 K-hash

K-hash Time Limit: 2000ms Memory Limit: 131072KB This problem will be judged on ZJU. Original ID: 389164-bit integer IO format: %lld      Java class name: Main K-hash is a simple string hash function. It encodes a string Sconsist of digit characters

ZOJ 2671 -Cryptography ( 矩阵乘法 + 线段树 )

ZOJ 2671 - Cryptography ( 矩阵乘法 + 线段树 ) 题意: 给定模数r, 个数n, 询问数m 然后是n个矩阵,每次询问,输出矩阵联乘之后的结果. 分析: 矩阵乘法 + 线段树优化 这里线段树只有询问没有更新操作. PS:顺便仰慕一下watashi.... 代码: #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using names