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 }
1289 - LCM from 1 to n
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


Problem Setter: Jane Alam Jan
思路:
有个定理
这个是如果n+1是素数的次方,那么当前的L(1+n)=L(n)*p,p为素数,否则就是L(n);
这个很好理解,如过n+1=(p)k,我们从最小公倍数的定义:Lcm=max(a1,a2,a3....)*max(b1,b2,b3...)*max(c1,c2,c3...)*....
其中a1是A1的某个素因数的个数,a2是A2的某个素因数的个数(这两个素因数相同).....
这样我们知道p这个素因数,在n时最大为k-1,所以当到n+1时就有L(n+1)=L(n)*p;否则的话如果n+1不是某个素数的次方那么代表着已经出现的所有素数的最大个数未被更新
那么就有L(n+1)=L(n);所以我们将【2,1e8】的素数全部筛选出来,由于内存限制所以只能用位图来筛选。
这样筛好后,然后我们把<=(1e8)的素数的次方打表出来,然后排序,这样再打表下到每个素数次方时(1,pk)的LCM;然后每次查找二分就行。
  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的更多相关文章

  1. LightOj 1289 - LCM from 1 to n(LCM + 素数)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1289 题意:求LCM(1, 2, 3, ... , n)%(1<<32), ...

  2. Light 1289 - LCM from 1 to n (位图标记+素数筛选)

    题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1289 题目描述: 给出一个n,求出lcm(1,2,3......n)为多少? ...

  3. LightOJ 1289 LCM from 1 to n(位图标记+素数筛

    https://vjudge.net/contest/324284#problem/B 数学水题,其实就是想写下位图..和状压很像 题意:给n让求lcm(1,2,3,...,n),n<=1e8 ...

  4. 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 ...

  5. CodeBlocks及LCM应用

    以下是在开发过程中遇到的一些细节点: 1)called after throwing an instance of std::bad_alloc 此问题是由于publish(data),当中data赋 ...

  6. LCM 轻量级通信组件

    LCM和ZMQ比较 http://www.doc88.com/p-6711552253536.html 基于LCM和ZeroMQ的进程间通信研究 2.简介 LCM(Lightweight Commuc ...

  7. uva12546. LCM Pair Sum

    uva12546. LCM Pair Sum One of your friends desperately needs your help. He is working with a secret ...

  8. UVA 10791 Minimum Sum LCM(分解质因数)

    最大公倍数的最小和 题意: 给一个数字n,范围在[1,2^23-1],这个n是一系列数字的最小公倍数,这一系列数字的个数至少为2 那么找出一个序列,使他们的和最小. 分析: 一系列数字a1,a2,a3 ...

  9. LCM在Kernel中的代码分析

    lcm的分析首先是mtkfb.c 1.mtk_init中platform_driver_register(&mtkfb_driver)注册平台驱动 panelmaster_init(); DB ...

随机推荐

  1. ysoserial-CommonsBeanutils1的shiro无依赖链改造

    ysoserial-CommonsBeanutils1的shiro无依赖链改造 一.CB1利用链分析 此条利用链需要配合Commons-Beanutils组件来进行利用,在shiro中是自带此组件的. ...

  2. c/c++在线编译Output Limit Exceeded(OLE)错误

    提示输出错误,有如下两个可能情况: 1. 不符合题目给出的输出格式,自己输出了多余的内容或者格式不正确 2. 输入数据的时候,未考虑到输入错误的情况 针对2,有如下的例子: 错误的情况: 1 int ...

  3. Java项目发现==顺手改成equals之后,会发生什么?

    最近发生一件很尴尬的事情,在维护一个 Java 项目的时候,发现有使用 == 来比较两个对象的属性, 于是顺手就把 == 改成了 equals.悲剧发生...... == 和 equals 的区别 = ...

  4. Linux 【复习巩固】

    目录 一.网络和服务 1.查看ip 2.查看主机名 配置 3.临时服务 1)基本语法(CentOS 6) 2)基本语法(CentOS 7) 3)示例 4.开机自启动服务 1)基本语法(CentOS 6 ...

  5. Vue 之keep-alive的使用,实现页面缓存

    什么是keep-alive 有时候我们不希望组件被重新渲染影响使用体验: 或者处于性能考虑,避免多次重复渲染降低性能.而是希望组件可以缓存下来,维持当前的状态.这时候就需要用到keep-alive组件 ...

  6. css通配样式初始化(多款,供君自选)

    腾讯官网 body,ol,ul,h1,h2,h3,h4,h5,h6,p,th,td,dl,dd,form,fieldset,legend,input,textarea,select{margin:0; ...

  7. Vue API 3 (模板语法 ,指令)

    条件 v-if v-if 指令用于条件性地渲染一块内容.这块内容只会在指令的表达式返回 truthy 值的时候被渲染. v-show v-show 指令也是用于根据条件展示一块内容.v-show 只是 ...

  8. Java资源下载

    tomcat http://mirror.bit.edu.cn/apache/tomcat/tomcat-8/v8.5.49/bin/apache-tomcat-8.5.49.tar.gz     s ...

  9. jquery对radio和checkbox的操作

    jQuery获取Radio选择的Value值 代码  $("input[name='radio_name'][checked]").val(); //选择被选中Radio的Valu ...

  10. Spring事务隔离级别和传播特性(转)

    相信每个人都被问过无数次Spring声明式事务的隔离级别和传播机制吧!今天我也来说说这两个东西. 加入一个小插曲,一天电话里有人问我声明式事务隔离级别有哪几种,我就回答了7种,他问我Spring的版本 ...