我是靠谱客的博主 动听短靴,最近开发中收集的这篇文章主要介绍HDU 4913 Least common multiple 解题报告(线段树)Least common multiple,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Least common multiple

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


Problem Description
bobo has an integer set S={x 1,x 2,…,x n}, where x i=2 ai * 3 bi.

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

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

The first line contains n (1≤n≤10 5). Each of the following n lines contain 2 integers a i,b i (0≤a i,b i≤10 9).
 

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
 

Author
Xiaoxu Guo (ftiasch)
 

Source
2014 Multi-University Training Contest 5

    解题报告:思路和官方的思路是一样的。使用线段树维护 b 这边的和。

    官方解题报告路径:blog.sina.com.cn/s/blog_6bddecdc0102uz53.html

    代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <iomanip>
using namespace std;
#define ff(i, n) for(int i=0;i<(n);i++)
#define fff(i, n, m) for(int i=(n);i<=(m);i++)
#define dff(i, n, m) for(int i=(n);i>=(m);i--)
#define bit(n) (1LL<<(n))
typedef long long LL;
typedef unsigned long long ULL;
const LL inf=1e15;
void work();
int main()
{
#ifdef ACM
freopen("input.in", "r", stdin);
//
freopen("input.in", "w", stdout);
#endif // ACM
work();
}
/***************************************************/
const int maxn = 111111;
const int mod = 1e9+7;
int n;
struct Node
{
int a, b;
bool operator<(const Node & cmp) const
{
return a == cmp.a ? b < cmp.b : a < cmp.a;
}
} node[maxn];
double bb[maxn];
int tot;
int powMod(LL a, int b)
{
LL res = 1;
while(b)
{
if(b&1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
#define lson l, m, pos<<1
#define rson m+1, r, pos<<1|1
#define defm int m = (l+r)/2
int sum[maxn<<2];
int mul[maxn<<2];
void updateFather(int pos)
{
sum[pos] = (sum[pos<<1] + sum[pos<<1|1]) % mod;
}
void updateSon(int pos)
{
if(mul[pos] > 1)
{
mul[pos<<1] = (LL)mul[pos<<1] * mul[pos] % mod;
sum[pos<<1] = (LL)sum[pos<<1] * mul[pos] % mod;
mul[pos<<1|1] = (LL)mul[pos<<1|1] * mul[pos] % mod;
sum[pos<<1|1] = (LL)sum[pos<<1|1] * mul[pos] % mod;
mul[pos] = 1;
}
}
void updateP(int p, int v, int l, int r, int pos)
{
if(l == r)
{
sum[pos] = (LL)v * mul[pos] % mod;
return ;
}
updateSon(pos);
defm;
if(p<=m)
updateP(p, v, lson);
else
updateP(p, v, rson);
updateFather(pos);
}
void update(int L, int R, int l, int r, int pos)
{
if(L<=l && r<=R)
{
mul[pos] = mul[pos] * 2 % mod;
sum[pos] = sum[pos] * 2 % mod;
return;
}
updateSon(pos);
defm;
if(L<=m)
update(L, R, lson);
if(m<R)
update(L, R, rson);
updateFather(pos);
}
int query(int L, int R, int l, int r, int pos)
{
if(L<=l && r<=R)
return sum[pos];
updateSon(pos);
defm;
return ((L<=m?query(L,R,lson):0) + (m<R?query(L,R,rson):0))%mod;
}
void work()
{
while(scanf("%d", &n) == 1)
{
tot = 0;
ff(i, n) scanf("%d%d", &node[i].a, &node[i].b), bb[tot++] = node[i].b;
sort(node, node + n);
sort(bb, bb + tot);
double sub = 0.999999;
double eve = 0.000001;
memset(sum, 0, sizeof(sum));
ff(i, maxn<<2) mul[i] = 1;
LL ans = 0;
ff(i, n)
{
LL now = powMod(2, node[i].a);
int pos = lower_bound(bb, bb+tot, node[i].b) - bb;
bb[pos] -= sub, sub -= eve;
updateP(pos, powMod(3, node[i].b), 0, tot, 1);
ans += now * query(pos, tot, 0, tot, 1) % mod;
update(pos+1, tot, 0, tot, 1);
}
printf("%dn", ans%mod);
}
}

最后

以上就是动听短靴为你收集整理的HDU 4913 Least common multiple 解题报告(线段树)Least common multiple的全部内容,希望文章能够帮你解决HDU 4913 Least common multiple 解题报告(线段树)Least common multiple所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(78)

评论列表共有 0 条评论

立即
投稿
返回
顶部