1289 - LCM from 1 to n
http://blog.csdn.net/acdreamers/article/details/18507767
这个是位图的链接,这篇写的挺好。
模板:
1 #include<math.h>
2 #include<stdlib.h>
3 #include<stdio.h>
4 #include <algorithm>
5 #include<iostream>
6 #include<string.h>
7 #include<vector>
8 #include<map>
9 #include<math.h>
10 using namespace std;
11 typedef long long LL;
12 typedef unsigned long long ll;
13 int cmp(const void*p,const void*q);
14 const int N=1e8;
15 const int M=5;
16 const int V=(1<<M)-1;
17 int prime[(N>>M)+4]= {0};
18 void setbit(LL x)
19 {
20 prime[x>>M]|=1<<(x&(V));
21 }
22 bool getbit(LL x)
23 {
24 return prime[x>>M]&(1<<(x&V));
25 }
26 int kp[7000000];
27 int main(void)
28 {
29 int i,j,k;LL p;
30 for(i=2; i<=20000; i++)
31 {
32 if(!getbit(i))
33 {
34 for(j=i; i*j<=100000000; j++)
35 {
36 setbit(i*j);
37 }
38 }
39 }int ans=0;
40 for(i=2;i<=100000000;i++)
41 {
42 if(!getbit(i))
43 {
44 kp[ans++]=i;
45 }
46 }
47 return 0;
48 }
Time Limit: 4 second(s) | Memory Limit: 64 MB |
Given an integer n, you have to find
lcm(1, 2, 3, ..., n)
lcm means least common multiple. For example lcm(2, 5, 4) = 20, lcm(3, 9) = 9, lcm(6, 8, 12) = 24.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case starts with a line containing an integer n (2 ≤ n ≤ 108).
Output
For each case, print the case number and lcm(1, 2, 3, ..., n). As the result can be very big, print the result modulo 232.
Sample Input |
Output for Sample Input |
5 10 5 200 15 20 |
Case 1: 2520 Case 2: 60 Case 3: 2300527488 Case 4: 360360 Case 5: 232792560 |
1 #include<math.h>
2 #include<stdlib.h>
3 #include<stdio.h>
4 #include <algorithm>
5 #include<iostream>
6 #include<string.h>
7 #include<vector>
8 #include<map>
9 #include<math.h>
10 #include<queue>
11 using namespace std;
12 typedef long long LL;
13 typedef unsigned long long ll;
14 const int N=1e8+2;
15 const int M=5;
16 const int V=(1<<M)-1;
17 const LL mod=4294967296;
18 typedef struct node
19 {
20 unsigned int id;
21 unsigned int NN;
22
23 } ss; bool cmp( struct node p,struct node q)
24 {
25 return p.NN<q.NN?true:false;
26 }
27 ss io[6000000];
28 int prime[(N>>M)+4]= {0};
29 void setbit(LL x)
30 {
31 prime[x>>M]|=1<<(x&(V));
32 }
33 bool getbit(LL x)
34 {
35 return prime[x>>M]&(1<<(x&V));
36 }
37 int er(int n,int m,int ans,int t);
38 int main(void)
39 {
40 int i,j,k;LL p;
41 for(i=2; i<=20000; i++)
42 {
43 if(!getbit(i))
44 {
45 for(j=i; i*j<=100000000; j++)
46 {
47 setbit(i*j);
48 }
49 }
50 }
51 int ans=0;
52 int cns=0;
53 for(i=2; i<100000000; i++)
54 {
55 if(!getbit(i))
56 {
57 LL sum=i;ans++;
58 while(sum<=N)
59 {
60 io[cns].id=i;
61 io[cns++].NN=sum;
62 sum*=i;
63 }
64 }
65 }sort(io,io+cns,cmp);
66 for(i=1;i<cns;i++)
67 {
68 io[i].id=(io[i-1].id*io[i].id)%mod;
69 }//freopen("data.in","r",stdin);
70 //freopen("wrong.out","w",stdout);
71 scanf("%d",&k);
72 int s;
73 for(s=1; s<=k; s++)
74 {
75 scanf("%lld",&p);printf("Case %d: ",s);
76 {int l,r;
77 l=0;
78 r=cns-1;
79 int ak=0;
80 int uu;
81 while(l<=r)
82 {
83 int c=(l+r)>>1;
84 if(io[c].NN<=p)
85 {
86 ak=c;
87 l=c+1;
88 }
89 else
90 r=c-1;
91 }
92 unsigned int sum1=io[ak].id;
93 printf("%u\n",sum1);}
94 }
95 return 0;
96 }
97 int er(int n,int m,int ans,int t)
98 { int l=(n+m)/2;if(l<0)return -1;
99 if(io[l].NN==ans)
100 {
101 return l;
102 }
103
104 if(io[l-1].NN<ans&&io[l].NN>ans)
105 {
106 return l-1;
107 } else if(n==m&&m==t)
108 return m;
109 else if(n==m)
110 return n-1;
111 else if(io[l-1].NN>=ans&&io[l].NN>ans)
112 {
113 return er(n,l-1,ans,t);
114 }
115 else if(io[l-1].NN<ans&&io[l].NN<ans)
116 {
117 return er(l+1,m,ans,t);
118 }
119 }
我这里两种二分,下面函数式的比较难把喔,写起来很恶心。
1289 - LCM from 1 to n的更多相关文章
- LightOj 1289 - LCM from 1 to n(LCM + 素数)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1289 题意:求LCM(1, 2, 3, ... , n)%(1<<32), ...
- Light 1289 - LCM from 1 to n (位图标记+素数筛选)
题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1289 题目描述: 给出一个n,求出lcm(1,2,3......n)为多少? ...
- LightOJ 1289 LCM from 1 to n(位图标记+素数筛
https://vjudge.net/contest/324284#problem/B 数学水题,其实就是想写下位图..和状压很像 题意:给n让求lcm(1,2,3,...,n),n<=1e8 ...
- LCM性质 + 组合数 - HDU 5407 CRB and Candies
CRB and Candies Problem's Link Mean: 给定一个数n,求LCM(C(n,0),C(n,1),C(n,2)...C(n,n))的值,(n<=1e6). analy ...
- CodeBlocks及LCM应用
以下是在开发过程中遇到的一些细节点: 1)called after throwing an instance of std::bad_alloc 此问题是由于publish(data),当中data赋 ...
- LCM 轻量级通信组件
LCM和ZMQ比较 http://www.doc88.com/p-6711552253536.html 基于LCM和ZeroMQ的进程间通信研究 2.简介 LCM(Lightweight Commuc ...
- uva12546. LCM Pair Sum
uva12546. LCM Pair Sum One of your friends desperately needs your help. He is working with a secret ...
- UVA 10791 Minimum Sum LCM(分解质因数)
最大公倍数的最小和 题意: 给一个数字n,范围在[1,2^23-1],这个n是一系列数字的最小公倍数,这一系列数字的个数至少为2 那么找出一个序列,使他们的和最小. 分析: 一系列数字a1,a2,a3 ...
- LCM在Kernel中的代码分析
lcm的分析首先是mtkfb.c 1.mtk_init中platform_driver_register(&mtkfb_driver)注册平台驱动 panelmaster_init(); DB ...
随机推荐
- lua table与json的之间的互相转换高性能c++实现
请自行约束两种语言数据结构语法上的不同,避开如下问题: 1.json本身不约束key是否符合一个编程语言中的变量名,所以编写用于和编程语言数据结构交互的json代码时应该注意key是否正确. 2.lu ...
- 微信小程序扫描普通二维码打开小程序的方法
很久没有写博客了,之前换了一份工作,很久没有做Android开发了,现在转做前端开发了,记录一下遇到的问题及解决的方法. 最近做微信小程序开发,遇到一个需求,后台管理系统生成的问卷和投票会有一个二维码 ...
- 学习java的第十三天
一.今日收获(前两天家里有事,博客都忘了发了,唉) 1.通过看哔哩哔哩看黑马程序员的教学视频,学习了java中的数据类型自动转换.强制转换及注意事项三节 2.简单看了看完全学习手册 二.今日问题 1. ...
- 商业爬虫学习笔记day2
1. get传参 (1)url中包含中文报错解决方法 urllib.request.quote("包含中文的url", safe = "string.printtable ...
- Ecshop 安装
参考 http://www.68ecshop.com/article-617.html ecshop的安装第一步:下载ecshop网店系统正式版安装包 我们可以来ecshop开发中心的官网(www.6 ...
- 100个Shell脚本——【脚本2】截取字符串
[脚本2]截取字符串 一.脚本 现有一个字符串如下: http://www.aaa.com/root/123.htm 请根据以下要求截取出字符串中的字符: 1.取出www.aaa.com/root/1 ...
- Linux学习 - ifconfig
ifconfig 1.功能 用来查看和配置网络设备,当网络环境发生改变时可通过此命令对网络进行相应的配置. 2.用法 ifconfig [网络设备] [参数] (1).参数 up 启动指定网络设备 ...
- SpringMVC注解详情
@Component.@Repository @Service.@Controller 看字面含义,很容易却别出其中三个: @Controller 控制层,就是我们的action层 @Service ...
- Spring Cloud服务离线
服务离线,即某服务不能对外提供服务了.服务离线的原因有两种:服务下架与服务下线.这两种方案都是基于Actuator监控器实现的. 服务下架:将注册到Eureka Server中的Eureka Clie ...
- 访问者模式(Visitor Pattern)——操作复杂对象结构
模式概述 在软件开发中,可能会遇到操作复杂对象结构的场景,在该对象结构中存储了多个不同类型的对象信息,而且对同一对象结构中的元素的操作方式并不唯一,可能需要提供多种不同的处理方式,还有可能增加新的处理 ...