The Most Complex Number URAL - 1748(寻找最大反素数)

    测试数据有点大,按照反素数性质剪枝

    反素数的两个性质:

    ①组成反素数的质因子一定是从2开始的一些连续质数,因为如果不是从2开始的连续质数的话,例如2*2*3=12不是反素数,2*3*7也不是反素数。

    ②n=2^t1*3^t2*5^t3*7^t4.....必然t1>=t2>=t3>=....

    所以按照性质②剪枝

 

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
typedef unsigned long long ULL;
const ULL INF = ~0ULL;
//vector<int>ve;
ULL n,ans;

int best;
int ve[16] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53};
void dfs(int deep , int limit , ULL temp, int num_type)
{
    if(temp > n) return ;
    if(best < num_type)
    {
        best = num_type , ans = temp;
    }
    if(best == num_type && ans > temp) ans = temp;
    for(int i = 1 ; i <= limit ; i++)
    {
        double cur = (double) temp;
        if(n < cur * ve[deep]) break;
        dfs(deep + 1 , i , temp *= ve[deep], num_type*(i+1));
    }
}
int main()
{
    int T;
    cin >> T;
    while(T--)
    {
        cin >> n;
        best = 0;
        ans = INF;
        dfs(0,60,1,1);
        cout << ans << " " << best << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zhaiqiming2010/article/details/80419573