码迷,mamicode.com
首页 > 其他好文 > 详细

neu 1694 Primorial vs LCM 数论

时间:2016-05-13 14:08:23      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

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
思路:显然是求小于一个素数的n次方小于N的贡献为那个素数的n-1次方;
   因为一次方是没用的,所以素数打表到sqrt(e14);
   求出前缀积,二分查找位置;
   注意超内存跟,得到贡献那里必须要double;
技术分享
#include<bits/stdc++.h>
using namespace std;
#define ll unsigned long long
#define mod 1000000007
#define inf 100000000000005
#define MAXN 10000010
//#pragma comment(linker, "/STACK:102400000,102400000")
int scan()
{
    int res = 0 , ch ;
    while( !( ( ch = getchar() ) >= 0 && ch <= 9 ) )
    {
        if( ch == EOF ) return 1 << 30 ;
    }
    res = ch - 0 ;
    while( ( ch = getchar() ) >= 0 && ch <= 9 )
        res = res * 10 + ( ch - 0 ) ;
    return res ;
}
vector<pair<ll ,ll > >v;
vector <ll>ans;
ll prime[MAXN],cnt;
bool vis[MAXN];
void Prime()
{
    cnt=0;
    memset(vis,0,sizeof(vis));
    v.push_back(make_pair(1LL,1LL));
    for(ll i=2;i<MAXN;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(ll j=0;j<cnt&&i*prime[j]<MAXN;j++)
        {
            vis[i*prime[j]]=1;
            if(i%prime[j]==0)
            break;
        }
    }
    sort(v.begin(),v.end());
    ans.push_back(1LL);
    for(int i=1;i<v.size();i++)
    ans.push_back((ans[i-1]*v[i].second)%mod);
}
int main()
{
    ll x,y,z,i,t;
    Prime();
    int T,cs=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%llu",&x);
        ll st=0;
        ll en=v.size()-1;
        while(st<en)
        {
            ll mid=(st+en)/2;
            if(v[mid].first>x)
            en=mid;
            else
            st=mid+1;
        }
        if(x>=v[st].first)
        printf("Case %d: %llu\n",cs++,ans[st]%mod);
        else if(x>=v[st-1].first)
        printf("Case %d: %llu\n",cs++,ans[st-1]%mod);
    }
    return 0;
}
View Code

 

 

neu 1694 Primorial vs LCM 数论

标签:

原文地址:http://www.cnblogs.com/jhz033/p/5487965.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!