玲珑学院OJ 1029 - Bob and Alice are playing factors (大整数分解)

题目点我点我点我



1029 - Bob and Alice are playing factors

Time Limit:2s Memory Limit:128MByte

Submissions:138Solved:48

DESCRIPTION
Bob and his girl friend Alice like factors very much.One day, Bob find a beautiful path: 4, 2, 1, Becauese 2 is the factor of 4 and 1 is the factor of 2.Alice is excited, says "why can't we find a longest path A1,A2,A3,...,Ak,Ai+1A1,A2,A3,...,Ak,Ai+1 is the factor of AiAi and Ai+1<AiAi+1<Ai, for all 1i<k1≤i<k.Bob is so stupid, so can you answer this question?And Bob also wants to know how many longest path he can get.
INPUT
First line is a positive integer TT , represents there are TT test cases.For each test case:First line includes one number A1(1A11018)A1(1≤A1≤1018).
OUTPUT
For the i-th test case , first output Case #i: in a single line.Then output the answer of i-th test case mod 109+7109+7,the first answer is the length of the longest path and the second answer is the numbers of longest path.
SAMPLE INPUT
14
SAMPLE OUTPUT
Case #1: 3 1




题目大意:给定 A1 找一个最长的序列 A1,A2,,Ak ,序列 A 满足 Ai+1 Ai 的因子,并且 Ai+1<Ai(1i<k) 。求最长序列的长度,以及有多少种最长序列


解题思路:大整数分解,最长序列的个数=所有质因子总数的全排列  /  每个质因子个数的全排列,因为是除法取模,用逆元。(亲测数组f开到100没问题……估计数据略水,而且时间居然比1e6还慢orz)


/* ***********************************************
┆  ┏┓   ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃       ┃ ┆
┆┃   ━   ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃       ┃ ┆
┆┃   ┻   ┃ ┆
┆┗━┓ 马 ┏━┛ ┆
┆  ┃ 勒 ┃  ┆      
┆  ┃ 戈 ┗━━━┓ ┆
┆  ┃ 壁     ┣┓┆
┆  ┃ 的草泥马  ┏┛┆
┆  ┗┓┓┏━┳┓┏┛ ┆
┆   ┃┫┫ ┃┫┫ ┆
┆   ┗┻┛ ┗┻┛ ┆
************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <bitset>
using namespace std;

#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)
#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)
#define pb push_back
#define mp make_pair
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define LL long long
#define ULL unsigned long long
#define MS0(X) memset((X), 0, sizeof((X)))
#define SelfType int
SelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);}
SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;}
#define Sd(X) int (X); scanf("%d", &X)
#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())
#define all(a) a.begin(), a.end()
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")

const int Times = 10;
const LL INF = (LL)1<<61;
const int N = 550;

LL n, m, ct, cnt;
LL mini, mina,minb,ans;
LL fac[N], num[N];

LL gcd(LL a, LL b)
{
    return b? gcd(b, a % b) : a;
}

LL multi(LL a, LL b, LL m)
{
    LL ans = 0;
    a %= m;
    while(b)
    {
        if(b & 1)
        {
            ans = (ans + a) % m;
            b--;
        }
        b >>= 1;
        a = (a + a) % m;
    }
    return ans;
}

LL quick_mod(LL a, LL b, LL m)
{
    LL ans = 1;
    a %= m;
    while(b)
    {
        if(b & 1)
        {
            ans = multi(ans, a, m);
            b--;
        }
        b >>= 1;
        a = multi(a, a, m);
    }
    return ans;
}

bool Miller_Rabin(LL n)
{
    if(n == 2) return true;
    if(n < 2 || !(n & 1)) return false;
    LL m = n - 1;
    int k = 0;
    while((m & 1) == 0)
    {
        k++;
        m >>= 1;
    }
    for(int i=0; i<Times; i++)
    {
        LL a = rand() % (n - 1) + 1;
        LL x = quick_mod(a, m, n);
        LL y = 0;
        for(int j=0; j<k; j++)
        {
            y = multi(x, x, n);
            if(y == 1 && x != 1 && x != n - 1) return false;
            x = y;
        }
        if(y != 1) return false;
    }
    return true;
}

LL pollard_rho(LL n, LL c)
{
    LL i = 1, k = 2;
    LL x = rand() % (n - 1) + 1;
    LL y = x;
    while(true)
    {
        i++;
        x = (multi(x, x, n) + c) % n;
        LL d = gcd((y - x + n) % n, n);
        if(1 < d && d < n) return d;
        if(y == x) return n;
        if(i == k)
        {
            y = x;
            k <<= 1;
        }
    }
}

void find(LL n, int c)
{
    if(n == 1) return;
    if(Miller_Rabin(n))
    {
        fac[ct++] = n;
        return ;
    }
    LL p = n;
    LL k = c;
    while(p >= n) p = pollard_rho(p, c--);
    find(p, k);
    find(n / p, k);
}

LL inv(LL a)
{
    return quick_mod(a, mod - 2, mod);
}

LL f[105];

void init()
{
    f[0] = 1;
    for(int i=1;i<=100;i++)
        f[i] = f[i-1] * i % mod;
}

int main()
{
    int t;
    t = read();
    int cas = 1;
    init();
    while(t--)
    {
        LL n;
        scanf("%lld",&n);

        ct = cnt = 0;
        find(n, 120);
        sort(fac, fac + ct);
        num[0] = 1;
        LL k = 1;
        for(int i=1; i<ct; i++)
        {
            if(fac[i] == fac[i-1])
                ++num[k-1];
            else
            {
                num[k] = 1;
                fac[k++] = fac[i];
            }
        }
        cnt = k;
        LL sum = f[ct];
        for(int i=0;i<cnt;i++)
            sum = sum*inv(f[num[i]])%mod;
        ct = (ct + 1)%mod;
        printf("Case #%d: %lld %lld\n",cas++,ct,sum);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值