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 ...
随机推荐
- lua脚本中字符串分割split
function split( s, c ) for item in string.gmatch( s, "(.-)"..c) do print(item); end end s ...
- SharePoint Solutions Deployment-PowerShell
之前群里有人问到了这个,项目一期开发结束后正好整理了一下,发上来分享一下,也是从谷歌搜索里面学来的,大家要用好搜索哈 $ver = $host | select version if ($ver.Ve ...
- JAVA多线程suspend()、resume()和wait()、notify()的区别
suspend() 和 resume() 方法:两个方法配套使用,suspend()使得线程进入阻塞状态,并且不会自动恢复,必须其对应的 resume() 被调用,才能使得线程重新进入可执行状态.典型 ...
- 如何调用在$(function(){ //内部函数代码 });
这个文件主要完成如何调用在jquery内部定义的函数,主要有两种方法 法①: <script type="text/javascript"> $(function() ...
- 基于Log4net插件
基本代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using log ...
- 通过VNC Viewer使用VMware虚拟机的远程桌面连接
本文转自:http://www.14blog.com/archives/185 要在VMware虚拟机中使用远程桌面连接?方法有两个:一种是在虚拟机中做“端口映射”,当然,这个稍显复杂(虚拟机端口映射 ...
- JAX-WS创建和处理SOAP消息
JAX-WS规范是一组XML web services的JAVA API,在 JAX-WS中,一个远程调用可以转换为一个基于XML的协议例如SOAP,在使用JAX-WS过程中,开发者不需要编写任何生成 ...
- 基于node.js构建微服务中的mock服务
缘起 由于现在微服务越来越火了,越来越多的微服务融入到了日常开发当中.在开发微服务的时候,经常会遇到一个问题由于依赖于其他服务,导致你的进度受到阻碍.使你不得不先mock出你期望调用依赖服务的输出,来 ...
- ajax获取后台传递的json数据
最近在使用JQuery的ajax方法时,需要返回的数据为json数据,在success返回中数据处理会根据返回方式不同会采用不同的方式来生成json数据.在$.ajax方法中应该是如何来处理的,简 ...
- iOS之崩溃处理:This application is modifying the autolayout engine from a background thread
一.错误提示 今天在开发的时候遇到一个崩溃问题,"This application is modifying the autolayout engine from a background ...