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 ...
随机推荐
- Nginx学习笔记4 源码分析
Nginx学习笔记(四) 源码分析 源码分析 在茫茫的源码中,看到了几个好像挺熟悉的名字(socket/UDP/shmem).那就来看看这个文件吧!从简单的开始~~~ src/os/unix/Ngx_ ...
- android动画特效之解决解决移动后闪烁现象,解决输入法弹出后位置回复原状,解决两个动画叠加
以下代码实现的效果是: BoundOpenView从居中移动到顶部,移动完后,BoundSendView从隐藏变为显示,并从顶部移动BoundOpenView下方20dp处,同时透明度慢慢增加. pr ...
- Jenkins安装plugin
Jenkins支持很多的plugin,这些plugin极大地丰富了Jenkins的功能.安装plugin有两种方式:自动安装和手动安装. 1.自动安装 这种方式非常简单,但前提是Jenkins必须连接 ...
- 微信内嵌H5网页 解决js倒计时失效
项目要求:将H5商城页面嵌套到公司微信公众号里 项目本身的开发跟移动端网页并无太多差异,只是这昨天遇到一个问题,说是棘手,到也简单. 用户下单后,在选择支付方式页面,有个倒计时的逻辑(从下单时开始计算 ...
- spring_boot打jar包及打包错误的解决方法
问题产生及解决 对于刚接手spring_boot的人,可能会感觉到很茫然无措,看到书上写的spring_boot是直接打包成jar包,使用java -jar *运行.然而却不知道该怎么打包.而创建sp ...
- CentOS6.5编译安装Python-2.7
zlib.x86_64 * yum install zlib.x86_64 openssl-devel.x86_64 * yum install openssl-devel.x86_64 Python ...
- 关于CachedRowSetImpl类
全称: com.sun.rowset.CachedRowSetImpl 父类: javax.sql.rowset.BaseRowSet 实现的接口: javax.sql.RowSet javax.sq ...
- Docker集群实验环境布署--swarm【3 注册服务监控与自动发现组件--consul】
参考官网集群配置方式 https://hub.docker.com/r/progrium/consul/ 集群中需要manager与node能通信consul的发现服务,不然,管理节点选举不了,无 ...
- CART分类与回归树与GBDT(Gradient Boost Decision Tree)
一.CART分类与回归树 资料转载: http://dataunion.org/5771.html Classification And Regression Tree(CART)是决策 ...
- JSON 与 对象 、集合 之间的转换
JSON字符串和java对象的互转[json-lib] 在开发过程中,经常需要和别的系统交换数据,数据交换的格式有XML.JSON等,JSON作为一个轻量级的数据格式比xml效率要高,XML需要很 ...