• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
2014>
博客园    首页    新随笔    联系   管理    订阅  订阅
浙江省赛 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\n",(sum+mod)%mod);
    }
    return 0;
}

 

posted on 2018-05-08 12:42  2014>  阅读(110)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3