题目链接:https://vjudge.net/problem/LightOJ-1341
思路:开始想了
n
\sqrt{n}
n的暴力算法,发现复杂度大概在4e9显然不能过。考虑分解质因数(试除法复杂度也不够,Pollard Rho 算法
n
1
/
4
n^{1/4}
n1/4复杂度可行),我们对分解完的质因数进行dfs枚举原数中所有因数(因数个数期望位
l
n
(
n
)
ln(n)
ln(n)),进行计数。复杂度期望
O
(
n
1
/
4
+
l
n
(
n
)
)
O(n^{1/4}+ln(n))
O(n1/4+ln(n))。
代码:
#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
using namespace std;
ll max_factor;
struct BigIntegerFactor {
const static int N = 1e6 + 7;
const static ll inf=0x3f3f3f3f3f3f3f3f;
ll prime[N], p[N], fac[N], sz, cnt; //多组输入注意初始化cnt = 0
inline ll mul(ll a, ll b, ll mod) { //WA了尝试改为__int128或慢速乘
if (mod <= 1000000000)
return a * b % mod;
return (a * b - (ll)((long double)a / mod * b + 1e-8) * mod + mod) % mod;
}
void init(int maxn) //线性筛
{
int tot = 0;
sz = maxn - 1;
for (int i = 1; i <= sz; ++i)
p[i] = i;
for (int i = 2; i <= sz; ++i) {
if (p[i] == i)
prime[tot++] = i;
for (int j = 0; j < tot && 1ll * i * prime[j] <= sz; ++j) {
p[i * prime[j]] = prime[j];
if (i % prime[j] == 0)
break;
}
}
}
ll qpow(ll a, ll x, ll mod)
{
ll res = 1ll;
while (x) {
if (x & 1)
res = mul(res, a, mod);
a = mul(a, a, mod);
x >>= 1;
}
return res;
}
bool check(ll a, ll n) { //二次探测原理检验n
ll t = 0, u = n - 1;
while (!(u & 1))
t++, u >>= 1;
ll x = qpow(a, u, n), xx = 0;
while (t--) {
xx = mul(x, x, n);
if (xx == 1 && x != 1 && x != n - 1)
return false;
x = xx;
}
return xx == 1;
}
bool miller(ll n, int k) {
if (n == 2)
return true;
if (n < 2 || !(n & 1))
return false;
if (n <= sz)
return p[n] == n;
for (int i = 0; i <= k; ++i) { //测试k次
if (!check(rand() % (n - 1) + 1, n))
return false;
}
return true;
}
inline ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a % b);
}
inline ll Abs(ll x) {
return x < 0 ? -x : x;
}
ll Pollard_rho(ll n) { //基于路径倍增的Pollard_Rho算法
ll s = 0, t = 0, c = rand() % (n - 1) + 1, v = 1, ed = 1;
while (1) {
for (int i = 1; i <= ed; ++i) {
t = (mul(t, t, n) + c) % n;
v = mul(v, Abs(t - s), n);
if (i % 127 == 0) {
ll d = gcd(v, n);
if (d > 1)
return d;
}
}
ll d = gcd(v, n);
if (d > 1)
return d;
s = t;
v = 1;
ed <<= 1;
}
}
void getfactor(ll n) { //得到所有的质因子(可能有重复的)
if (n <= sz) {
while (n != 1)
fac[cnt ++ ] = p[n], n /= p[n];
max_factor = max_factor > p[n] ? max_factor : p[n];
return;
}
if (miller(n, 6)) {
fac[cnt ++ ] = n;
max_factor = max_factor > n ? max_factor : n;
}
else {
ll d = n;
while (d >= n)
d = Pollard_rho(n);
getfactor(d);
getfactor(n / d);
}
return ;
}
ll ans,num;
pair<ll,ll> pp[N];
void dfs(ll x,ll a,ll b,ll now)
{
if(x>=num)
{
//printf("ans:%lld %lld %lld\n",x,now,a/now);
if(min(now,a/now)>=b)
{
if(now!=a/now) ans++;
//else ans+=2;//(注意不能是正方形)
}
return ;
}
dfs(x+1,a,b,now);
for(int i=1;i<=pp[x].se;i++)
{
now*=pp[x].fi;
dfs(x+1,a,b,now);
}
}
//这样写不太好初始化。。。。WA了好久
ll slove(ll a,ll b)
{
cnt=0;ans=0;num=0;
getfactor(a);
sort(fac,fac+cnt);
fac[cnt]=-1;
pp[num].se=0;
for(int i=0;i<=cnt;i++)
{
pp[num].fi=fac[i];
pp[num].se++;
if(fac[i]!=fac[i+1])
{
num++;
pp[num].se=0;
}
}
dfs(0,a,b,1);
return ans/2;
}
}Q;
int main()
{
ll t;
scanf("%lld",&t);
ll cas=0;
while(t--)
{
ll a,b;
scanf("%lld%lld",&a,&b);
if(a==1||b>a/b)
{
printf("Case %lld: 0\n",++cas);
continue;
}
printf("Case %lld: %lld\n",++cas,Q.slove(a,b));
}
}