nowcoder N约数个数

本文介绍了一种算法,通过使用前20个质数并递减质数指数的方法来搜索在[1,n]区间内拥有最多约数的数,并给出具体的C++实现代码。

n的约数个数

题目:t次询问,每次给你一个数n,求在[1,n]内约数个数最多的数的约数个数

数据:对于100%的数据,t <= 500 , 1 <= n <= 1000000000000000000

思路:对前20个质数搜索,质数指数依次递减。

代码:

 1 #include "bits/stdc++.h"
 2 
 3 #define ll  long long
 4 using namespace std;
 5 const int mod=1e9+7;
 6 const int N=1e6+5;
 7 int a[20]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53};
 8 
 9 ll ans,n;
10 void dfs(int p,int up,ll res,ll now){
11     if(res>ans) ans=res;
12     for(int tot=1;tot<=up&&now<=n/a[p];tot++){
13         now*=a[p];
14         dfs(p+1,tot,res*(tot+1),now);
15     }
16 }
17 int main() {
18     int t;
19     cin>>t;
20     while(t--){
21         cin>>n;
22         ans=1;
23         dfs(0,63,1,1);
24         cout<<ans<<endl;
25     }
26 }

转载于:https://www.cnblogs.com/mj-liylho/p/7183293.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值