2014多校5(1003)hdu4913(线段树区间操作)

本文探讨了一个关于集合中元素的最小公倍数(LCM)的编程问题,提出了一种利用线段树进行计数的方法,以解决给定特定形式整数集合的所有非空子集的LCM之和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Least common multiple

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 451    Accepted Submission(s): 151


Problem Description
bobo has an integer set S={x1,x2,…,xn}, where xi=2ai * 3bi.

For each non-empty subsets of S, bobo added the LCM (least common multiple) of the subset up. Find the sum of LCM modulo (109+7).
 

Input
The input consists of several tests. For each tests:

The first line contains n (1≤n≤105). Each of the following n lines contain 2 integers ai,bi (0≤ai,bi≤109).
 

Output
For each tests:

A single integer, the value of the sum.
 

Sample Input
2 0 1 1 0 3 1 2 2 1 1 2
 

Sample Output
11 174

题意:RT

思路:最小公倍数只能是形如2^a * 3^b,而每次求一种最小公倍数 2^i * 3^j 的和只需要考虑小于等于i 和 j 的集合总数就好了,这个计数可以用线段树来完成,按a从小到大的顺

序将b插入线段树(注意b要先离散化),对于相同的a在插入b的时候也要按照b从小到大插入,这是为了保证后面比b大的数还是在a之前插入的,然后可以用两个数组分别维护

小于等于b的集合数 f[o] 和 小于b的集合数 ff[o],然后f[o]-ff[o]就是此时等于b的集合数了,累加就好了,这题也是把我恶心了好久,sad~~

#include 
#include 
#include 
#include 
#include 
using namespace std;
#define maxn 100010
#define lson l,m,o<<1
#define rson m+1,r,o<<1|1
#define mod (1000000000+7)

typedef __int64 ll;
ll f[maxn<<2];
ll ff[maxn<<2];
ll tree[maxn<<2];
ll treef[maxn<<2];

struct node{
    int a;
    int b;
    ll ax;
    ll bx;
};

node vex[maxn];

int n;

void pushdown(int o)
{
    if(f[o]!=1)
    {
        f[o<<1]=f[o<<1]*f[o]%mod;
        f[o<<1|1]=f[o<<1|1]*f[o]%mod;
        tree[o<<1]=tree[o<<1]*f[o]%mod;
        tree[o<<1|1]=tree[o<<1|1]*f[o]%mod;
        f[o]=1;
    }
    if(ff[o]!=1)
    {
        ff[o<<1]=ff[o<<1]*ff[o]%mod;
        ff[o<<1|1]=ff[o<<1|1]*ff[o]%mod;
        treef[o<<1]=treef[o<<1]*ff[o]%mod;
        treef[o<<1|1]=treef[o<<1|1]*ff[o]%mod;
        ff[o]=1;
    }
}

void pushup(int o)
{
    tree[o]=(tree[o<<1]+tree[o<<1|1])%mod;
    treef[o]=(treef[o<<1]+treef[o<<1|1])%mod;
}

void build(int l,int r,int o)
{
    f[o]=ff[o]=1;
    if(l==r){
        tree[o]=treef[o]=0;
        return;
    }
    int m=l+r>>1;
    build(lson);
    build(rson);
    pushup(o);
}

void insert1(int l,int r,int o,int L,int R,int vis)
{
    if(L<=l&&r<=R)
    {
        if(vis==1){
        f[o]=f[o]*2%mod;
        tree[o]=2*tree[o]%mod;
        }
        else {
        ff[o]=ff[o]*2%mod;
        treef[o]=2*treef[o]%mod;
        }
        return;
    }
    pushdown(o);
    int m=l+r>>1;
    if(L<=m)insert1(lson,L,R,vis);
    if(m>1;
    ll ans=0;
    if(L<=m)ans=(ans+query(lson,L,R,vis))%mod;
    if(m>1;
    if(p<=m)update(lson,p,x);
    else update(rson,p,x);
    pushup(o);
}

bool cmp(node a,node b)
{
    if(a.a==b.a)return a.b
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值