題解/算法 {200. Hankson的趣味题}

Link

Solution

If you can focus on the two-information G c d ( x , a 0 ) = a 1 , L c m ( x , b 0 ) = b 1 Gcd(x,a0) = a1, Lcm(x, b0) = b1 Gcd(x,a0)=a1,Lcm(x,b0)=b1, it would be very hard…

A vital hint is x ∣ b 1 x | b1 xb1, so the solution actually very simple:

int ans = 0;
for( x : $(all Divisors of `b1`){
	if( Gcd(x, a0) == a1 && Lcm(x, b0) == b1){
		++ ans;
	}
}

Cuz the Divisor-Count of int at most 1600 1600 1600, the Time-Cost of this method is negligible.

The only thing is, how you get All-Divisors for a int;
+ If the familiar-mean is O ( N ) = 5 e 4 O(\sqrt{N}) = 5e4 O(N )=5e4, T ( 2000 ) ∗ 5 e 4 T(2000) * 5e4 T(2000)5e4 would Out-Of-Time;
+ Here we use another-mean for get All-Divisors:
. 1 Get the Prime-Factorization using a special-algorithm: O ( N ln ⁡ N ) + O ( N ) O(\frac{\sqrt{N}}{\ln{\sqrt{N}}}) + O(\sqrt{N}) O(lnN N )+O(N ) (it is a Template-Algorithm)
. . It resembles to the Normal-Method for( i = 2; i <= x / i; ++i), while in here, i i i will be replaced by all Prime-Numbers in the range ≤ x \leq \sqrt{x} x ;
. 2 Cuz we have already get the Prime-Factorization of this number, we can use DFS exhaustedly to build all its Divisors; (the Time-Cost equals to the Divisor-Count 1600 1600 1600)

Finally, the Time-Cost is 2000 ∗ ( O ( N ln ⁡ N ) + 1600 + 1600 ∗ ln ⁡ N ) + O ( N ) = 2000 ∗ O ( N ln ⁡ N ) ( 5 e 3 ) = = 1 e 7 2000 * ( O(\frac{\sqrt{N}}{\ln{\sqrt{N}}}) + 1600 + 1600 * \ln{N}) + O(\sqrt{N}) = 2000 * O(\frac{\sqrt{N}}{\ln{\sqrt{N}}})(5e3) = = 1e7 2000(O(lnN N )+1600+1600lnN)+O(N )=2000O(lnN N )(5e3)==1e7

Code

Core-Episode (Prime-Factorization in O ( N ln ⁡ N ) O(\frac{\sqrt{N}}{\ln{\sqrt{N}}}) O(lnN N ))

{ //< @Snippet, Primes_Factorization
    auto temp = b1;
    for( int power, ind = 0; ind < P->Get_primesCount(); ++ind){
        auto prime = P->Primes[ ind];
        if( prime > temp / prime){ break;}
        if( temp % prime == 0){
            power = 0;
            while( temp % prime == 0){
                ++ power;
                temp /= prime;
            }
            PrimeFactors[ M ++] = {prime, power};
        }
    }
    if( temp > 1){
        PrimeFactors[ M ++] = {temp, 1};
    }
}

Use DFS(O(1600)) to build all-Divisors according to @Name(PrimeFactors);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值