[BZOJ3733]Iloczyn [BZOJ3733]Iloczyn

BZOJ
搜索烂得要死
首先分解质因数,从小到大排序,爆搜
把不合法的全return之后,一个剪枝:
计算还要几个,如果从当前开始往后这么多个数乘起来再乘s>n就return(可行性剪枝)
BZOJ机子慢的要死

#define ll long long
#include<bits/stdc++.h>
using namespace std;
int re(){
    int x=0,w=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*w;
}
int T,n,k,tot,z[40000],mu[40000];
bool ans;
void fact(int x){
    for(int i=1,sq=sqrt(x);i<=sq;i++)
        if(x%i==0){
            z[++tot]=i;
            if(i*i^x)z[++tot]=x/i;
        }
}
void dfs(int x,int y,ll s){
    if(x==k+1){if(s==n)ans=1;return;}
    if(s>n||ans||y>tot||tot-y<k-x)return;
    ll mu=1;
    for(int i=y;i<=y+k-x;i++){
        mu*=z[i];if(s*mu>n)return;
    }
	dfs(x,y+1,s);if(ans)return;
	dfs(x+1,y+1,s*z[y]);
}
int main(){
    T=re();
    while(T--){
        n=re(),k=re();tot=0;fact(n);
        ans=0;
        sort(z+1,z+tot+1);
        dfs(1,1,1);
        puts(ans?"TAK":"NIE");
    }
    return 0;
}