题意:如标题

思路:如果n在10^6以内则可以用o(nlogn)的暴力,题目给定的是n<=1e8,暴力显然是不行的,考虑到1到n的最小公倍数可以写成2^p1*3^p2*5^p3*...这种素数的幂的乘积形式,对于当前询问n,可知质数x的指数为(int)log(x,n)(因为要满足是公倍数且最小),因为前n个数有n/logn个质数,这样一次处理为o(n/logn*logn) = o(n)的。由于有T组测试数据,直接T次处理肯定会超时,需要离线处理。具体怎么操作呢?首先将T个询问按n从小到大排序,从小到大处理时,每个质数的指数是非递减的,所以只需在上一次的答案上乘以若干质数。如果记录上一个询问后的每个质数的指数,然后遍历所有质数,看质数有没有增加,这样的时间复杂度为o(T*n/logn+n/logn*logn)=o(Tn/logn),跟直接T次在线处理没什么两样,原因是有很多的质数的指数并不会变化,却也被访问了一次。

一种解决办法是预先计算出每个询问n所增加的质数,从小到大枚举每个质数的每个幂,然后在T个询问中二分,得到第一次出现这个幂的n,这样时间复杂度为o(n/logn*logn*logT)=o(nlogT),查询时就相当于是只查了那个最大的n一样,所以查询复杂度为o(n),总复杂度为o(nlogT),虽然过不了此题,但这个思路却可以对付一些其他T比较大,n稍小的数据。

下面讲另一种o(Tsqrt(n)+n)的思路:核心思想是将质数分为两类,一类是小于等于sqrt(n),一类大于sqrt(n),不难发现对于第二类质数,它们的质数要么是1要么是0,也就是说,对于第二类质数,假设它在某个询问n时加进了答案,那么在以后的询问中就不用再考虑了(如果继续加进答案,那么它的指数会超过1),于是不难得到如下算法:对每个询问,枚举第一类质数,判断他们的质数有没有增加(实际操作时用试乘法而不是求一个对数),而第二类质数只需维护当前乘到了哪个质数就行了。下面是代码:

 #pragma comment(linker, "/STACK:10240000,10240000")

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <ctime>
#include <cctype>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <stdexcept>
#include <utility> using namespace std; #define mem0(a) memset(a, 0, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define define_m int m = (l + r) >> 1
#define rep_up0(a, b) for (int a = 0; a < (b); a++)
#define rep_up1(a, b) for (int a = 1; a <= (b); a++)
#define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
#define rep_down1(a, b) for (int a = b; a > 0; a--)
#define all(a) (a).begin(), (a).end()
#define lowbit(x) ((x) & (-(x)))
#define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
#define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
#define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
#define pchr(a) putchar(a)
#define pstr(a) printf("%s", a)
#define sstr(a) scanf("%s", a)
#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d%d", &a, &b)
#define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pint(a) printf("%d\n", a)
#define test_print1(a) cout << "var1 = " << a << endl
#define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
#define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a) typedef unsigned int uint;
typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> vi; const int dx[] = {, , -, , , , -, -};
const int dy[] = {-, , , , , -, , - };
const int maxn = 1e8 + ;
const int md = ;
const int inf = 1e9 + ;
const LL inf_L = 1e18 + ;
const double pi = acos(-1.0);
const double eps = 1e-; template<class T>T gcd(T a, T b){return b==?a:gcd(b,a%b);}
template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
template<class T>T condition(bool f, T a, T b){return f?a:b;}
template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
int make_id(int x, int y, int n) { return x * n + y; } int vis[];
unsigned int prime[]; int c; void init() {
const int t = 1e8 + ;
for (int i = ; i <= t; i ++) {
int u = i >> , v = i & ;
if (vis[u] & ( << v)) continue;
prime[c ++] = i;
if ((LL)i * i > t) continue;
for (int j = i * i; j <= t; j += i) {
int u = j >> , v = j & ;
vis[u] |= << v;
}
}
} pii node[];
unsigned int out[];
const int max_sq = 1e4;
unsigned int last[]; int main() {
//freopen("in.txt", "r", stdin);
int T, cas;
cin >> T;
cas = T;
init();
while (T --) {
int n;
sint(n);
node[cas - T - ] = make_pair(n, cas - T - );
}
sort(node, node + cas);
unsigned int ans = ;
int n = node[].first;
int cur = ;
for (int i = ; prime[i] < max_sq; i ++) last[i] = ;
rep_up0(i, cas) {
int n = node[i].first;
for (int j = ; prime[j] < max_sq; j ++) {
while ((LL)last[j] * prime[j] <= n) {
last[j] *= prime[j];
ans *= prime[j];
}
}
while (prime[cur] <= n) {
cur ++;
if (prime[cur - ] < max_sq) continue;
ans *= prime[cur - ];
}
out[node[i].second] = ans;
}
rep_up0(i, cas) cout << out[i] << endl;
return ;
}
11巨带我飞 

[acdream_oj1732]求1到n的最小公倍数(n<=1e8)的更多相关文章

  1. 算法 - 求两个自然数的最小公倍数(C++)

    //************************************************************************************************** ...

  2. C# 求俩个正整数的最小公倍数和最大公约数

    C# 求俩个正整数的最小公倍数和最大公约数 1.公倍数.最小公倍数 两个或多个整数公有的倍数叫做它们的公倍数,其中除0以外最小的一个公倍数就叫做这几个整数的最小公倍数 翻开小学5年级下册PPT 1.1 ...

  3. 代码代码:输入两个正整数m和n,求其最大公约数和最小公倍数。15 20 5

    import java.util.Scanner; //输入两个正整数m和n,求其最大公约数和最小公倍数.15 20 5 public class Test { public static void ...

  4. Python实现利用最大公约数求三个正整数的最小公倍数示例

    Python实现利用最大公约数求三个正整数的最小公倍数示例 本文实例讲述了Python实现利用最大公约数求三个正整数的最小公倍数.分享给大家供大家参考,具体如下: 在求解两个数的小公倍数的方法时,假设 ...

  5. java求最大公约数,和最小公倍数

    import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = ...

  6. JS求一个数组元素的最小公倍数

    求几个数的最小公倍数就是先求出前两个数的最小公倍数,然后再把这个最小公倍数跟第三个数放在一起来求最小公倍数,如此类推... var dbList = []; //两个数的最小公倍数 function ...

  7. vjudge 最大公约数GCD 直接求最大共约束和最小公倍数的指令

    原题链接https://vjudge.net/contest/331993#problem/C 输入2个正整数A,B,求A与B的最大公约数. Input2个数A,B,中间用空格隔开.(1<= A ...

  8. 辗转相除 求最大公约数!or 最小公倍数

    求最大公约数和最小公倍数的经典算法--辗转相除法描述如下: 若要求a,b两数的最大公约数和最小公倍数,令a为a.b中较大数,b为较小数,算法进一步流程: while(b不为0) { temp=a%b: ...

  9. 输入两个正整数m和n,求其最大公约数和最小公倍数

    public static void main(String[] args){  Scanner sc = new Scanner (System.in);  int a,b;  System.out ...

随机推荐

  1. Mac 安装 brew(最新教程,绝对可行)

    现在安装brew,一会报这个错,一会儿报那个错,上网查了很多教程,用了很多时间都是不可以,电脑开VPN翻墙也不行. Warning: The Ruby Homebrew installer is no ...

  2. Win10桌面美化

    捯饬了几个小时终于捯饬好了,没什么特效,就是看起来干净了许多. 用到的小软件: 链接:https://pan.baidu.com/s/1_PSTn0JZ22ZGiMDOdvdWEw提取码:329c 1 ...

  3. How to check if directory exist using C++ and winAPI

    如果看文件夹是否存在,必须看返回值是不是 INVALID_FILE_ATTRIBUTES #include <windows.h> #include <string> bool ...

  4. python教程(目录)

    很早就想出一套python的零基础入门教程,各种原因一直没动手.今天立个flag,2020年一定完成这个目标. 入门篇 完全零基础的小白应该从这里看起. 一.计算机原理 这里不是要让大家去深入的学习计 ...

  5. 【Linux常见问题】CentOS 7 root用户密码忘记,找回密码方法

    1.开机按esc 2.选择CentOS Linux (3.10.0-693.......)     按 e 键: 3.光标移动到 linux 16 开头的行,找到 ro 改为 rw init=sysr ...

  6. 打造更好用的 EF 自动审计

    打造更好用的 EF 自动审计 Intro 上次基于 EF Core 实现了一个自动审计的功能,详细可以参考 https://www.cnblogs.com/weihanli/p/auto-audit- ...

  7. 3年前的一个小项目经验,分享给菜鸟兄弟们(公文收发小软件:小技能 SmallDatetime)...

    为什么80%的码农都做不了架构师?>>>   这个系统中的数据库有100多M,里面当然有很多表,我的每个表里,有几个字段,都是一样的例如 CreateUserID.CreateDat ...

  8. Netty随记之ChannelInboundHandlerAdapter、SimpleChannelInboundHandler

    ChannelInboundHandlerAdapter ChannelInboundHandlerAdapter是ChannelInboundHandler的一个简单实现,默认情况下不会做任何处理, ...

  9. 蚂蚁金服合作的RISE实验室到底有多牛?

    近日,蚂蚁金服与美国加州伯克利大学近期新成立的RISE实验室达成合作意向.RISE实验室的前身是著名伯克利AMP实验室,主导研发了当今大数据计算领域最前沿的开源系统:Apache Spark.Apac ...

  10. C++课程设计详解-12306的模拟实现

    目录 设计思路... 3 思路分析:.... 3 数据组织:.... 4 具体功能实现过程... 4 管理端具体功能实现:.... 4 用户端具体功能实现:.... 5 调试截图和调试过程中遇到的问题 ...