zoukankan      html  css  js  c++  java
  • 浙江省赛 ZOJ4029

    Now Loading!!!
    Time Limit: 1 Second      Memory Limit: 131072 KB
    DreamGrid has  integers . DreamGrid also has  queries, and each time he would like to know the value of
    for a given number , where , .
    
    Input
    There are multiple test cases. The first line of input is an integer  indicating the number of test cases. For each test case:
    
    The first line contains two integers  and  () -- the number of integers and the number of queries.
    
    The second line contains  integers  ().
    
    The third line contains  integers  ().
    
    It is guaranteed that neither the sum of all  nor the sum of all  exceeds .
    
    Output
    For each test case, output an integer , where  is the answer for the -th query.
    
    Sample Input
    2
    3 2
    100 1000 10000
    100 10
    4 5
    2323 223 12312 3
    1232 324 2 3 5
    Sample Output
    11366
    45619
    Author: LIN, Xi
    Source: The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple
    Submit    Status

    这题前缀和+二分

    我们发现分母只有1-30;

    我们构造一个sum/i(i-30)

    二分查找 a^(i -1)<p<a^(i)  分母 就是 i 

    你就把a[i]分成30段。

    用flag 去保存位子。

    每一段就是 k[j][flag[j]]-k[j][flag[j-1]]

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    const int maxn=500005;
    const int mod=1e9;
    int  a[maxn],p[maxn],k[35][maxn];
    int n,m;
    int main()
    {
        int t,flag[maxn],cnt;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d %d",&n,&m);
            for(int i=1; i<=n; i++)
                scanf("%d",&a[i]);
            sort(a+1,a+n+1);
            for(int i=1; i<=m; i++)
                scanf("%d",&p[i]);
            for(int i=1; i<=30; i++)
            {
                k[i][0]=0;
                for(int j=1; j<=n; j++)
                {
                    k[i][j]=(k[i][j-1]+(a[j]/i))%mod;
                }
            }
            ll sum=0,ans;
            ll temp;
            for(int i=1; i<=m; i++)
            {
                temp=1;
                cnt=0;
                ans=0;
                while(temp*p[i]<=a[n])
                {
                    temp*=p[i];
                    int l=1,r=n;
                    while(l<=r)
                    {
                        int mid=(l+r)/2;
                        if(a[mid]<=temp)
                            l=mid+1;
                        else
                            r=mid-1;
                    }
                    flag[++cnt]=r;
                }
                if(flag[cnt]<n)
                    flag[++cnt]=n;
                for(int j=1; j<=cnt; j++)
                {
                    ans=(ans+(k[j][flag[j]]-k[j][flag[j-1]]))%mod;
                }
                sum=(sum+ans*i)%mod;
            }
            printf("%lld
    ",(sum+mod)%mod);
        }
        return 0;
    }
  • 相关阅读:
    MySQL分区性能初探
    FastDFS开源的轻量级分布式文件系统
    MySQL数据类型之数值类型,对理解类型定义中的“位”有莫大的帮助
    空密码引发共享打印机拒绝访问
    利用Myxls导出并下载Excel
    StyleCop SA0102
    Spring AOP介绍
    大学英语一下重修听力考试范围.doc 听力原文 及MP3
    Eclipse中文版
    飞鱼秀下载
  • 原文地址:https://www.cnblogs.com/2014slx/p/9007556.html
Copyright © 2011-2022 走看看