白井黑子【Wannafly挑战赛29-B】

题目链接


  蛮好的一道题,补题的时候用了O(N) 的算法给过了,还有一种O(N^2)的算法应该是指的是树状数组,一会我去尝试下,这道题,在比赛的时候,我想到的就是上次做过的DNA那道题类似,开一个多维树状数组,然后去查对应的补集,然后可惜的是当时硬是没想出来BUG在哪,因为题目有些坑点:

  • K==0时候,我们取余就会取不到,所以要特殊的分出来算;
  • 还有一件事,当你K==0时候知道分开来算,也小心我们的得到的数位f()也只有1时才是不能利用的,非1还是都可以用的。

  剩下的思维:你读代码吧,就是简单的找补集,然后找对应关系即可。


#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;
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;
}

 

一会,我再去尝试下用下树状数组去做一下这道题,蛮好的一道题,多几种方法去弄。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值