1694: Primorial vs LCM 数论

链接:戳这里

1694: Primorial vs LCM

时间限制: 4 Sec  内存限制: 128 MB

[提交][状态][讨论版]

题目描述

Given N (2<=N<=10^14), what is the quotient of LCM(1,2,3,....,N) divided by multiple of all primes up

to N. As the result might be too big, output it‘s modulo by 1000000007.

For example, when N=5, the result is LCM(1,2,3,4,5)/(2*3*5)=60/30=2.

Note that LCM stands for Lowest or Least Common Multiple.

输入

The first line of the input is T(T ≤ 50000), then T test cases follows in next T lines. Each line

contains an integer N (2 ≤ N ≤ 100000000000000 or 10^14). The meaning of N is given in the

problem statement.

输出

For each test case print a line in “Case x: S” format where x is case number and S is the

quotient modulo by 1000000007.

样例输入

10

2

3

4

5

6

7

8

9

10

1000

样例输出

Case 1: 1

Case 2: 1

Case 3: 2

Case 4: 2

Case 5: 2

Case 6: 2

Case 7: 4

Case 8: 12

Case 9: 12

Case 10: 744593350

题意:求出LCM(1~n)/(年以内的素数乘积)

思路:LCM(1~n) = n以内的每个素数的能取的最高次幂的乘积

LCM(1,2,3,4,,,49) = 2^5 * 3^3 * 5^2 * 7^2 * 11 *13...

发现LCM(1~49) 除一下 49以内的素数 = 2^4 * 3^3 * 5  * 7

这样只需要计算1e7以内的素数然后取每个素数的最高次幂-1,就可以求出n=1e14的情况  但是还是会T

由于一段连续的n的答案是一样的 所有来模拟一下过程 (这里的结论是没有当出现一个新的最高次幂的素数的时候才会改变LCM的值

2 1

3 1

4 2

8 4

9 12

16 24

25 120

prime^num anw[上一项]*prime

下面来分析分析

想一下这是什么原因呢  因为每次只有得到新的prime得最高次幂才会对LCM产生变化

所以每次的答案更新也就在prime^num这里

哎  没意思  然后在线性筛那里记录值 然后二分输出答案

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 10000005
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const ll INF=1e14+5;
#define mod 1000000007
int T,cas;
vector< pair<ll,int> > V;
vector<ll> anw;
int vis[MAX],prime[MAX];
void init(){
    int cnt=0;
    V.push_back(make_pair(1LL,1));
    for(int i=2;i<=MAX;i++){
        if(!vis[i]){
            prime[cnt++]=i;
            for(double j=(double)i*i;j<INF;j*=i){
                V.push_back(make_pair((ll)j,i));
            }
        }
        for(int j=0;j<cnt;j++){
            if((ll)i*prime[j]>MAX) break;
            vis[i*prime[j]]=1;
            if(i%prime[j]==0) break;
        }
    }
    sort(V.begin(),V.end());
    anw.push_back(1LL);
    for(int i=1;i<V.size();i++){
        anw.push_back((ll)V[i].second*anw[i-1]%mod);
    }
}
ll n;
int main(){
    init();
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++){
        scanf("%lld",&n);
        int l=0,r=V.size()-1,mid,ans=0;
        while(l<r){
            mid=(l+r+1)/2;
            if(V[mid].first<=n) {
                l=mid;
                ans=mid;
            }
            else r=mid-1;
        }
        printf("Case %d: %lld\n",cas,anw[r]%mod);
    }
    return 0;
}
时间: 2024-07-29 19:39:15

1694: Primorial vs LCM 数论的相关文章

neu 1694 Primorial vs LCM 数论

1694: Primorial vs LCM 时间限制: 4 Sec  内存限制: 128 MB[提交][状态][讨论版] 题目描述 Given N (2<=N<=10^14), what is the quotient of LCM(1,2,3,....,N) divided by multiple of all primes up to N. As the result might be too big, output it's modulo by 1000000007. For exam

HDU4497 GCD and LCM 数论 素数分解

题意很简单首先以前做最简单的LCM跟CGD的时候都知道先求出两个数A,B的最大公约数GCD,那么LCM可以利用  A*B/GCD来求得,这点一开始脑残了没想到,结果没有进行特盘所以错了,意思就是 题目给的L%G不为0的话就是无解,结果我给判其它的去了,肯定漏了些什么没有发现 然后对于 L/G进行素因子分解,同时任意的数都能够通过素因子分解来表示,所以三个解x,y,z也能分解 L/G = p1^q1*p2^q2.... x = p1^i1*... y = p1^j1*... z = p1^k1*.

Uva 11388 GCD LCM ( 数论 )

Uva  11388 GCD LCM( 数论 ) 题意: 求是否存在a,b 使得lcm(a,b) = L, gcd(a,b) = G,不存在输出-1,存在输出a,b,且a尽可能小 分析: 强行暴力是不可能的数据很大,要用llu,这里有两种思路 思路一: 由题意可知 a*b = G*L 保证a = G的倍数的情况下,枚举a再判断G*L能否整除a,最后判断b是否为a的倍数.a从G开始扫到sqrt(G*L) //输入两个整数G,L //找出a,b 使得 gcd(a,b) = G lcm(a,b) =

NEU1694: Primorial vs LCM

链接:http://acm.neu.edu.cn/hustoj/problem.php?id=1694 题意:给定多个n,求LCM(1~n)/pi{pi为1~n中的素数}. 分析:因为n太大有10^14,我们得观察一些性质才行.因为要求的是最小公倍数然后除掉所有的质数,这里很明显大于sqrt(n)的素数就没意义了,因为最后答案中留下的只能是指数大于1的素数.那么我们就将素数范围缩小到了10^7,然后我们再来看看有什么其他的性质,我们会知道素数p在答案中的贡献应该是p^k<=n<p^(k+1)时

hdu 4497 GCD and LCM 数论 素数分解

GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1339    Accepted Submission(s): 607 Problem Description Given two positive integers G and L, could you tell me how many solutions of

UVA 10791 Minimum Sum LCM 数论

题目链接: https://vjudge.net/problem/UVA-10791 题目描述: 给一个数n, 让你求至少两个数的lcm是n 的, 最小和 解题思路: 唯一分解, 每个单独的素数的幂加起来就是答案 代码: #include <iostream> #include <cstdio> #include <string> #include <vector> #include <cstring> #include <iterator

LightOJ 1215 Finding LCM(数论)

题意  已知LCM(a, b, c) = L 和 a.b.L   求最小的满足等式的c. 把数展开为素因子积的形式后 GCD(a,b)就是a,b的公共素因子取在a.b中的较小指数 LCM(a,b)就是a,b的所有素因子取在a.b中的较大指数 令m = LCM(a,b)  那么问题转化为了求最小的c满足 LCM(m, c) = L 那么最小的c就是L中不在m中的素因子和L中指数大于m中指数的素因子取在L中的指数即积 #include <bits/stdc++.h> using namespace

UVA 10791 Minimum Sum LCM (数论)

LCM (Least Common Multiple) of a set of integers is defined as the minimum number, which is a multiple of all integers of that set. It is interesting to note that any positive integer can be expressed as the LCM of a set of positive integers. For exa

数论入门2——gcd,lcm,exGCD,欧拉定理,乘法逆元,(ex)CRT,(ex)BSGS,(ex)Lucas,原根,Miller-Rabin,Pollard-Rho

数论入门2 另一种类型的数论... GCD,LCM 定义\(gcd(a,b)\)为a和b的最大公约数,\(lcm(a,b)\)为a和b的最小公倍数,则有: 将a和b分解质因数为\(a=p1^{a1}p2^{a2}p3^{a3}...pn^{an},b=p1^{b1}p2^{b2}p3^{b3}...pn^{bn}\),那么\(gcd(a,b)=\prod_{i=1}^{n}pi^{min(ai,bi)},lcm(a,b)=\prod_{i=1}^{n}pi^{max(ai,bi)}\)(0和任何