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)时的p^(k-1),并且明显会有小质数的贡献质数k>=大质数的k,有了这些我们就能处理出2^2,2^3,3^3等等这些数了,比如:(4,2),(8,2),(9,3),(16,2),(25,5)。。。假设n=10,那我们找到小于它的第一个值(9,3)那么答案就是2*2*3啦,当n=26时找到(25,5)答案就是2*2*3*2*5啦。详见代码。

代码:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=10000010;
const int MAX=1000000100;
const int mod=100000000;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000007;
const int INF=1000000010;
typedef double db;
typedef unsigned long long ull;
struct node {
    ll x,y,z;
    node(){}
    node(ll x,ll y):x(x),y(y){}
    bool operator < (const node a) const{
        return x<a.x;
    }
}f[700000];
int g,a[N],q[N];
void deal(ll n) {
    int i,j,k=0;
    ll mul;
    memset(q,0,sizeof(q));
    for (i=2;i<=n;i++) {
        if (!q[i]) a[++k]=i;
        for (j=1;j<=k;j++) {
            if ((ll)a[j]*i>n) break ;
            q[a[j]*i]=1;
            if (i%a[j]==0) break ;
        }
    }
    n=n*n;
    for (i=1;i<=k;i++) {
        mul=(ll)a[i]*a[i];
        f[++g]=node(mul,a[i]);
        while (log(mul)+log(a[i])<=log(n)) {
            f[++g]=node(mul*a[i],a[i]);mul*=a[i];
        }
    }
    sort(f+1,f+g+1);f[1].z=2ll;
    for (i=2;i<=g;i++) f[i].z=f[i-1].z*f[i].y%MOD;
}
ll getans(ll n) {
    if (n<4) return 1ll;
    if (n>=f[g].x) return f[g].z;
    int l=1,r=g,mid=(l+r)>>1;
    while (l+1<r)
    if (f[mid].x>n) { r=mid;mid=(l+r)>>1; }
    else { l=mid;mid=(l+r)>>1; }
    return f[l].z;
}
int main()
{
    int i,t,ca;
    ll n,ans;
    deal(10000000ll);
    scanf("%d", &t);
    for (ca=1;ca<=t;ca++) {
        scanf("%lld", &n);
        printf("Case %d: %lld\n", ca, (getans(n)+MOD)%MOD);
    }
    return 0;
}
时间: 2024-10-11 03:03:21

NEU1694: 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

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.

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): 1092    Accepted Submission(s): 512 Problem Description Given two positive integers G and L, could you tell me how many solutions of (

题目1439:Least Common Multiple(求m个正数的最小公倍数lcm)

题目链接:http://ac.jobdu.com/problem.php?pid=1439 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: // // 1439 Least Common Multiple.cpp // Jobdu // // Created by PengFei_Zheng on 10/04/2017. // Copyright © 2017 PengFei_Zheng. All rights reserved. /

Codeforces 348B:Apple Tree(DFS+LCM+思维)

http://codeforces.com/contest/348/problem/B 题意:给一棵树,每个叶子结点有w[i]个苹果,每个子树的苹果数量为该子树所有叶子结点苹果数量之和,要使得每个结点的各个子树苹果数量相等,求至少需要拿走的苹果数量. 思路:一开始以为只要使得所有子树之和相同就行了. 1 void dfs(int u, int fa) { 2 int num = 0, mi = INF; 3 for(int i = head[u]; ~i; i = edge[i].nxt) {

GCD AND LCM

1 #include <cstdio> 2 __int64 GCD(__int64 a,__int64 b) 3 { 4 if (a % b == 0) 5 return b; 6 else 7 return GCD(b,a%b); 8 } 9 __int64 LCM(__int64 a,__int64 b) // a * b ==gcd*lcm 10 { 11 return a / GCD(a,b) * b ; 12 } 13 int main() 14 { 15 __int64 a,b;

POJ 2429 GCD &amp; LCM Inverse

设答案为ans1,ans2 ans1=a1*gcd,ans2=a2*gcd,a1,a2互质 gcd*a1*b1=lcm,gcd*a2*b2=lcm a1*b1=lcm=(ans1*ans2)/gcd=a1*a2 综上所诉,a1=b2,a2=b1. 也就是说,ans1=gcd*k1,ans2=gcd*k2 要求k1,k2尽量接近,并且k1,k2互质,并且,k2*k2=lcm/gcd 需要用到Pollard_rho分解质因数,然后暴力搜索寻找k1,k2.用了kuangbin大神的Pollard_rh

【数论】[CF258C]Little elephant and LCM

题目 分析:枚举最大数,然后找出它所有因数p1--.pk, 从中任意选取一些数,这些数的LCM|这个数且,这些数的最大LCM就是枚举的这个数,且若pi<=aj<=pi+1则前i个数可以放在j这个位置,即j这个位置有cj种选择,总方案数就是c1*c2*--*cj 作为优化,对于每个pi,我们枚举有aj满足pi<=aj<=pi+1的个数记为qi,则有ans=1^qi*2^qi*--*q^qk,但这些方案包含不选择最大数的情况,则最后一项应为q^qk-(q-1)^qk 代码: #incl

Minimum Sum LCM(uva10791+和最小的LCM+推理)

L - Minimum Sum LCM Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 10791 题意:输入正整数n,<注意n=2^31-1是素数,结果是2^31已经超int,用long long,>找至少两个数,使得他们的LCM为n且要输出最小的和: 思路:既然LCM是n,那么一定是n的质因子组成的数,又要使和最小,那么就是ans+=[