ACdream 1732
input
样例个数T <=10000
每个样例一个n(2<=n<=10^8)
output
lcm(1,2,...,n)%2^32
Sample Input
5
10
5
200
15
20
Sample Output
2520
60
2300527488
360360
232792560 做法:质因分解,每个不大于n的质数不大于n的最高次幂相乘结果即为lcm(1,2,...,n)的结果
#include <bits/stdc++.h>
#define MAX 10000
#define INF 100000000
#define LL long long
#define U unsigned
using namespace std;
int cas=,T;
U int prime[],pn,*r,rn,*rr,n,*rrp;
struct node
{
U int x,y;
bool operator<(const node&a)const
{
return x>a.x;
}
};
/*void init() //TLE
{
pn=1;
bool *vis=new bool[INF+10];
priority_queue<node>q;
for(int i=2;i<=MAX;i++)
if(!vis[i])
{
for(int j=i*i;j<=INF;j+=i) vis[j]=1;
q.push(node{i,i});
}
prime[0]=1;
for(U int i=MAX+1,x=1;i<=INF;i++)
{
if(!vis[i]) prime[pn++]=i;
}
prime[pn++]=INF+10;
delete []vis;
r=new U int[pn+10];
r[0]=1;
for(int i=1;i<pn;i++) r[i]=r[i-1]*prime[i];
U int res=1;
rr=new U int[3000];
rrp=new U int[3000];
rr[0]=rrp[0]=1;
rn=1;
while(!q.empty())
{
node u=q.top();q.pop();
rrp[rn]=u.x;
rr[rn++]=res*u.y;
if((U LL)u.x*u.y<=INF) q.push(node{u.x*u.y,u.y});
}
rrp[rn++]=INF+10;
for(int i=1;i<rn;i++) rr[i]=rr[i-1]*rr[i];
}
*/
void init() //最大数为10000^2,则大于10000的素数最小公倍数的最大次幂为1,小于一万时不定
{
bool *vis=new bool[INF+];
pn=;
for(U int i=;i<=INF;i++) //O(n)筛法
{
if(!vis[i]) prime[pn++]=i;
for(U int j=;j<pn&&i*prime[j]<=INF;j++)
{
vis[i*prime[j]]=;
if(i%prime[j]==) break;
}
}
delete []vis; //内存不足,用完即删
priority_queue<node>q;
r=new U int[pn];
for(int i=;i<pn;i++) //prime存质数,r存第i个质数对应的结果(还没算1~10000的部分)
{
if(prime[i]<=MAX) { r[i]=;q.push(node{prime[i],prime[i]}); }
else r[i]=r[i-]*prime[i];
}
U int res=;
rr=new U int[];
rrp=new U int[];
rr[]=rrp[]=;
rn=;
while(!q.empty()) //rr存质数的幂次升序,rrp存底数
{
node u=q.top();q.pop();
rrp[rn]=u.x;
rr[rn++]=res*u.y;
if((U LL)u.x*u.y<=INF) q.push(node{u.x*u.y,u.y});
}
rrp[rn++]=INF+;
for(int i=;i<rn;i++) rr[i]=rr[i-]*rr[i]; //将rrp乘起来作为1~10000的结果
}
int main()
{
//printf("%d\n",sizeof(vis)+sizeof(prime));
init();
// for(int i=0;i<rn;i++) printf("%u %u\n",rrp[i],rr[i]);
//freopen("1.in","w",stdout);
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
scanf("%d",&T);
while(T--)
{
scanf("%u",&n);
int i1=upper_bound(prime,prime+pn,n)--prime; //查找>10000的结果
int i2=upper_bound(rrp,rrp+rn,n)--rrp; //查找1~10000的结果
printf("%u\n",r[i1] * rr[i2]);
}
delete []r;
delete []rrp;
delete []rr;
return ;
}
ACdream 1732的更多相关文章
- ACdream 1214---矩阵连乘
ACdream 1214---矩阵连乘 Problem Description You might have noticed that there is the new fashion among r ...
- 矩阵乘法快速幂 codevs 1732 Fibonacci数列 2
1732 Fibonacci数列 2 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 在“ ...
- acdream.LCM Challenge(数学推导)
LCM Challenge Time Limit:1000MS Memory Limit:64000KB 64bit IO Format:%lld & %llu Submit ...
- acdream.Triangles(数学推导)
Triangles Time Limit:1000MS Memory Limit:64000KB 64bit IO Format:%lld & %llu Submit Stat ...
- acdream.A Very Easy Triangle Counting Game(数学推导)
A - A Very Easy Triangle Counting Game Time Limit:1000MS Memory Limit:64000KB 64bit IO Forma ...
- acdream.Bet(数学推导)
Bet Time Limit:1000MS Memory Limit:64000KB 64bit IO Format:%lld & %llu Submit Status Pra ...
- acdream.郭式树(数学推导)
郭式树 Time Limit:2000MS Memory Limit:128000KB 64bit IO Format:%lld & %llu Submit Status Pr ...
- ACdream 1188 Read Phone Number (字符串大模拟)
Read Phone Number Time Limit:1000MS Memory Limit:64000KB 64bit IO Format:%lld & %llu Sub ...
- ACdream 1195 Sudoku Checker (数独)
Sudoku Checker Time Limit:1000MS Memory Limit:64000KB 64bit IO Format:%lld & %llu Submit ...
随机推荐
- FileTable初体验
FileTable初体验 阅读导航 启用FILESTREAM设置 更改FILESTRAM设置 启用数据库非事务性访问级别 FileTable 在我接触FileTable之前,存储文件都是存储文件的链接 ...
- Windows应用商店API
Windows应用商店API 动手实验 实验 8: Windows应用商店API 2012年9月 简介 编写Windows应用商店应用最令人瞩目的理由之一是您可以方便地将它们发布到Windows应用商 ...
- 设置Linux可以查看历史命令的执行时间
大家都知道Linux平台上,可以通过history命令查看最近所执行过的命令,但history命令默认所显示的只有编号和命令的,只知道命令是最近所执行的,但不知具体执行的日期.时.分.秒,有时给寻找证 ...
- 简单的遮罩层效果,user登陆显示效果
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- js 数组常用的操作函数整理
平时多做企业应用开发,抱着实用为主,对前端技术理解得比较肤浅,下面就是肤浅地对 js 数组的属性和方法及对它操作的 jquery 方法做些记录: js 数组是 js 内建的一个非常强大数据类型,由于 ...
- C语言之位运算
位运算 1).定义. 指的是1个二进制数据的每一位来参与运算. 位运算的前提: 是这个数必须是1个二进制. 注意: a). 参与位运算的二进制数据必须是补码形式. b). 位运算的结果也是二进制的补码 ...
- 关于C/C++中的“auto”
C/C++ 98标准 C++03标准 意思完全一样:auto被解释为一个自动存储变量的关键字,也就是申明一块临时的变量内存.auto的出现意味着,当前变量的作用域为当前函数或代码段的局部变量,意味着当 ...
- 《如何阅读一本书》(How to Read a Book)
值得一读的书,有深入浅出,也有并不能完全读懂的部分,以下是第11章对之前内容的总结整理. 阅读的层次 1. 基础阅读 2. 检视阅读 3. 分析阅读 4. 主题阅读 分析阅读 第一阶段:这本书在谈些什 ...
- springmvc-interceptor(拦截器)
在大配置中配置拦截器代码如下: <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**" ...
- Linux 编译安装 源代码
编译安装 源代码包的安装一般为下载软件源代码,然后编译安装. 常见的C程序软件的安装步骤是 configure, make, make install三部曲,大致是下面这样操作: 首先得安装gcc.m ...