题目链接
蛮好的一道题,补题的时候用了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;
}
一会,我再去尝试下用下树状数组去做一下这道题,蛮好的一道题,多几种方法去弄。