Cattle off: t ask times, each time giving you a number n, find in [1, n] within a maximum number of divisor divisor number number (number theory + greedy)

https://ac.nowcoder.com/acm/contest/907/B

t interrogation times, each time you give a number n, find the [. 1 n,] within a maximum number submultiple of the number of count about

 

analysis:

The divisors Theorem: For a positive integer n greater than 1 can be prime factor decomposition: n = p1 ^ a1 * p2 ^ a2 * p3 ^ a3 * ... * pk ^ ak, by the divisor number of n of about positive theorem, there are number of (a₁ + 1) (a₂ + 1) (a₃ + 1) ... (ak + 1) th,

Violence calculate the number of divisors of each number, time out!

Based on the unique decomposition theorem, we know that every number can be represented by a product of the quality factor, while only about the number of the number of index-related!

We know that pn> ...> p3> p2> p1, then we assume that there is a certain ak> a1 and p1 pk we exchange index, the number of apparently about the same number, but the number of smaller! ! !

That is for any n, m if pn> pm then an <am better, is not optimal, not sure! But many have been eliminated for us.

We enumerate every prime number of prime factors to ensure that its index declining.

Original: https: //blog.csdn.net/wust_cyl/article/details/79774584

#include<bits/stdc++.h>
using namespace std;
#define ll unsigned long long
int pr[20] = {0,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,51};
ll n,ans;
ll qpow(int a , int b)
{
    if(b<0) return a;
    ll ret=1;
    while(b)
    {
        if(b&1) ret=ret*a;
        b>>=1;
        a=a*a;
    }
    return ret;
}
void dfs(int pos , ll num , ll sum , int len)
{
    if(sum>n) return ;
    if(sum<=n) ans=max(ans,num);

    for(int i=1 ; i<=len ; i++)
    {
        ll ret=qpow(pr[pos],i);
        if(sum>n/ret) break;
        dfs(pos+1,num*(i+1),sum*ret,i);
    }
}
int main()
{
    int t;scanf("%d",&t);
    while(t--){
            ans=0;
   cin>>n;
   dfs(1,1,1,30);
   cout<<ans<<endl;
    }
}
View Code

 




 

Guess you like

Origin www.cnblogs.com/shuaihui520/p/10961160.html