2012 Multi-University Training Contest 5-1006 hdu4344 Mark the Rope

该题是Pollard_rho分解质因数的模板题

1、所求长度是N的因子(且大于1小于N),集合中的元素得两两互质 

2、为了尽可能多的选出,每个L的质因子应当只包含N的一个质因子,L是一个质因子的整数次,所以K的值就是N中不同质因子的个数

3、要想和最大,那么使得每个L最大,只要使得质因子的指数最大即可

如果N本身是质数的话,直接输出1 1

用pollard_rho分解N的质因数,然后统计不同的质因子个数K,以及计算所有相同质因子乘积的和S

特殊情况:如果N本身是某个质数的整数i次幂,那么K只能等于1,因为L要小于N,所以L最大为该质数的i-1次幂 

以下是我用的pollard_rho的模板,效率不错~~


#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <map>
#include <iostream>
#include <iterator>
using namespace std;
#define MAXN 10
#define C 16381
//typedef __int64  I64;
typedef unsigned __int64 I64;
map<I64,int> fac;

//a*b可能会越界,用这个函数来实现乘法
I64 multi(I64 a, I64 b, I64 n){
    I64 tmp = a % n, s = 0;
    while(b){
        if(b & 1) s = (s + tmp) % n;
        tmp = (tmp + tmp) % n;
        b >>= 1;
    }
    return s;
}

//快速幂取模a的b次方
I64 Pow(I64 a, I64 b, I64 n){
    I64 tmp = a % n, s = 1;
    while(b){
        if(b & 1) s = multi(s, tmp, n);
        tmp = multi(tmp, tmp, n);
        b >>= 1;
    }
    return s;
}

//验证a的n-1次方
int witness(I64 a, I64 n){
    I64 u = n - 1, t = 0, i, x, y;
    while(!(u & 1))    u >>= 1, t++;
    x = Pow(a, u, n);
    for(i = 1; i <= t; i ++){
        y = multi(x, x, n);
        if(y == 1 && x != 1 && x != n -1) return 1;
        x = y;
    }
    if(x != 1) return 1;
    return 0;
}

//Miller_Rabbin法判断n是否为素数
int test(I64 n){
    I64 a;
    int i;
    if(n == 2) return 1;
    if(n < 2 || !(n & 1)) return 0;
    srand((I64)time(0));
    for(i = 0; i < MAXN; i ++){
        a = ((I64) rand()) % (n - 2) + 2;
        if(witness(a, n)) return 0;
    }
    return 1;
}

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

//pollard_rho算法核心
I64 pollard_rho(I64 n, I64 c){
    I64 x, y, d, i = 1, k = 2;
    srand((I64)time(0));
    x = ((I64) rand()) % (n - 1) + 1;
    y = x;
    while(1){
        i ++;
        x = (multi(x, x, n) + c) % n;
        d = gcd(y - x + n, n);
        if(d != 1 && d != n) return d;
        if(y == x) return n;
        if(i == k) y = x, k <<= 1;
    }
}

//把n分解质因数,c为循环调用随机算法的次数
void find(I64 n, I64 c){
    I64 r;
    if(n <= 1) return;
    if(test(n)){
        fac[n] ++;
        return;
    }
    r = pollard_rho(n, c--);
    find(n / r, c);
    find(r, c);
}

//调用find(n, C);

I64 mmm(I64 a,int b)
{
    I64 tmp =1;
    for(int i = 0;i < b;i++)
        tmp *= a;
    return tmp;
}

int main()
{
    int t,n;
    I64 num,ans;
    scanf("%d",&t);
    while(t--)
    {
        cin >> num;
        fac.clear();
        ans = 0;
        n = 0;
        if(test(num)){printf("1 1\n");continue;}
        find(num,120);
        map<I64,int>::iterator it;
        for(it = fac.begin();it != fac.end();it++)
        {
            n++;
            ans += mmm(it->first,it->second);
        }
        if(n==1)cout << n << " " << ans / (fac.begin()->first) << endl;
        else cout << n << " " << ans << endl;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值