zoukankan      html  css  js  c++  java
  • Now Loading!!!(ZOJ Problem Set

    The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple 

    F 题

    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

    题解:
    首先将数组从小到大排序,因为分母很少,最大为30,提前把a[i]/分母 求前缀和,枚举分母,然后二分
    若分母为i,二分pow(p,i-1) 和 pow(p,i) 的位置 根据位置把属于分母为 i 的区域求出来,若 pow(p,i-1) 超过 max(a[i]) 就break即可
    pow(p,i-1) 不用快速幂 , 从 1 一直乘 即可。

    时间复杂度为:O(n*30*log(n))

    额 ,,,, 没想到mod是1e9
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <algorithm>
    
    using namespace std;
    typedef long long ll;
    const int N=5*1e5+7;
    ll a[N],pre[32][N];
    const int mod=1e9;
    
    int main()
    {
        int t,n,m;
        ll maxx;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d",&n,&m);
            for(int i=1;i<=n;i++)
                scanf("%lld",&a[i]);
            a[0]=0;
            sort(a+1,a+n+1);
            for(int i=1;i<=31;i++)
            {
                pre[i][0]=0;
                for(int j=1;j<=n;j++)
                    pre[i][j]=(mod+pre[i][j-1]+(a[j]/i))%mod;
            }
            ll ans=0,p;
            maxx=a[n];
            for(int i=1;i<=m;i++)
            {
                scanf("%lld",&p);
                ll preNum=1,nowNum;
                ll res=0;
                for(int j=1;j<=31;j++)
                {
                    int nowInd,preInd;
                    if(preNum+1>maxx)  break;
                    nowNum=preNum*p;
                    if(nowNum+1<=maxx)
                        nowInd=lower_bound(a,a+n+1,nowNum+1)-a;
                    else
                        nowInd=n;
                    preInd=lower_bound(a,a+n+1,preNum+1)-a;
                    if(a[nowInd]>=nowNum+1)
                        nowInd--;
                   // cout<<nowInd<<" "<<nowNum<<endl;
                  //  cout<<preInd<<" "<<preNum<<endl;
                    if(nowInd>=preInd&&preInd>=1)
                    {
                        res=(res+mod+pre[j][nowInd]-pre[j][preInd-1])%mod;
                      //  cout<<j<<"*"<<(pre[nowInd]-pre[preInd-1])/j<<endl;
                      //  cout<<preInd-1<<"-->"<<nowInd<<endl;
                    }
                    preNum=nowNum;
                }
                ans=(ans+res*i%mod)%mod;
              //  printf("$%lld
    ",ans);
            }
            printf("%lld
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    好尚不可为,其况恶乎(转)
    getResource(String name)用法及源码分析
    怎样从ext3升级到ext4?
    Java设计模式之适配器模式(Adapter Pattern)
    javascript实现图片无缝滚动(scrollLeft的使用方法介绍)
    PowerDesigner使用教程
    python 多线程编程
    java中接口的定义与实现
    Java调用cmd命令 打开一个站点
    C#中MessageBox使用方法大全(附效果图)
  • 原文地址:https://www.cnblogs.com/lemon-jade/p/8974305.html
Copyright © 2011-2022 走看看