Wannafly 挑战赛29 B ——质因子分解+map

题目链接:https://ac.nowcoder.com/acm/contest/271/B

来源:牛客网 

觉得很好的一道题,既然是每位的数相乘,那么F【x】最后肯定可以分解为2,3,5,7四个质因子的分解形式,而题目中给的也是两数相乘能否表示成一个自然数(0-9)的k次幂的形式,所以,对于一个数,如果之后还存在一个数,他们的乘数结果含有上面四个质数的对于K次幂的补集,则对数加一,因此就可以想到使用map判断是否还存在补集,就不用暴力地去找了。最后注意判断一下k==0时,取余求补集不能实现,得特殊处理。

本人代码卡在了40%的地方,还不知道哪错了,这里借鉴一下大神的代码:https://blog.csdn.net/qq_41730082/article/details/84555750

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define efs 1e-6
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
/*
    每一个数都可以表示成2,3,5,7的因子分解式
    所以用四个因子的指数唯一表示每个数,存入map中
    寻找对数时,只需判断map中是否有相应的补集即可
    如,如果k=5,已有一个数为2^3,再判断2^2是否存在
*/

const int dir[4] = {2, 3, 5, 7};    //10内4个质数
ll N, K;
map<vector<ll>, int> mp;
vector<ll> vt(4), bu(4);   //开四倍空间 vt = vector<ll> (4);
ll get_real(ll x)
{
    if(x == 0) return 0;
    ll ans = 1;
    while(x)
    {
        ans = ans * (x%10);
        x/=10;
        if(ans == 0) return 0;
    }
    return ans;
}
int main()
{
    scanf("%lld%lld", &N, &K);
    if(K==0)
    {
        ll zero = 0;
        ll tmp;
        for(int i=1; i<=N; i++)
        {
            scanf("%lld", &tmp);
            tmp = get_real(tmp);
            if(tmp == 1) zero++;
        }
        printf("%lld\n", N*(N-1)/2 - zero*(zero-1)/2);
    }
    else
    {
        ll cnt = 0, zero = 0;
        ll tmp = 0;
        for(int i=1; i<=N; i++)
        {
            scanf("%lld", &tmp);
            tmp = get_real(tmp);
            if(tmp)
            {
                for(int i=0; i<4; i++)
                {
                    vt[i] = 0;
                    while(tmp%dir[i] == 0)
                    {
                        tmp/=dir[i];
                        vt[i]++;
                    }
                    vt[i]%=K;
                    bu[i] = (K - vt[i])%K;
                }
                cnt += mp[vt];
                mp[bu]++;
            }
            else zero++;
        }
        if(zero) cnt += zero*(zero-1)/2 + zero*(N-zero);
        cnt = N*(N-1)/2 - cnt;
        printf("%lld\n", cnt);
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值