Divisors
题意是说算一个区间内的因子数最大的数。网上题解大都是用的dfs,但我又不想写dfs······然后看了一下,区间长度最长才1e4,用质因数分解求出区间内每一个数的因子数然后用最朴素的方法找出答案应该也能过吧······事实证明真的可以,而且只用了40ms 2333333。
这里做个笔记,所谓质因数分解求因子数,指的是,对于一个数x,必定存在以下式子:
x = (a1^p1)*(a2^p2)*(a3^p3)*(a4^p4)*···*(an^pn)(其中,a1,a2,···,an均为质数)
那么,x的因子个数则为:
sum(x) = (1+p1)*(1+p2)*(1+p3)*(1+p4)*···*(1+pn)
比如说,对于12,有12 = (2^2) * (3^1) , 所以sum(12) = (1+2) * (1+1) = 6
此题的代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#include <iostream> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <list> #include <string> #include <queue> #define mst(a,b) memset(a,b,sizeof(a)) #define INF 0x3f3f3f3f using namespace std; typedef long long ll; typedef unsigned long long ull; const int maxn=1e6+5; int (int x) { if(x==1) return 1; if(x==2) return 2; ll ans=1;ll cnt=0; for(ll i=2;i*i<=x;i++) { cnt=0; while(x%i==0) { cnt++; x/=i; } ans*=(1+cnt); } return x>1?2*ans:ans; } ll maxi,pos; void get_ans(ll lef,ll rig) { maxi=-1; for(ll i=lef;i<=rig;i++) { ll tmp=get_cnt(i); if(tmp>maxi){ maxi=tmp; pos=i; } } } int main() { int t; cin>>t; while(t--) { ll lef,rig; cin>>lef>>rig; get_ans(lef,rig); printf("Between %lld and %lld, %lld has a maximum of %lld divisors.n",lef,rig,pos,maxi); } }
|
近期评论