ABC276 D题解
__vector__ · · 题解
做法
想一想除法操作的本质。
对于一个数除以
举个简单的例子,
除以
而所有数相等,实际上就是所有数的每种因数的数量分别相等。
而现在只能让每个数的因数
所以最后操作完的时候,如果取最优值,那么最终所有数的每种因数数量,都是这种因数在所有数之中出现次数的最小值。
所以先记录下因数
设第
答案就是
那无解怎么判定?
如果两个数因数
但是如果还出现了别的因数呢?
如果还出现了别的因数,那么这个因数必须在所有的数中出现次数相同,否则无解,因为无法调整。
最后复杂度,扫一遍,扫的过程中每个数计算对数次。
Code
#include <bits/stdc++.h>
using namespace std;
const int maxn=1005;
int n;
int a[maxn];
struct Cnt
{
int tw,thr,els;
}cnt[maxn];
int main()
{
scanf("%d",&n);
int es=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
while(a[i]!=1)
{
if(a[i]%2 && a[i]%3 && es!=0 && a[i]!=es)
{
printf("-1\n");
return 0;
}
if(a[i]%2&&a[i]%3)
{
es=a[i];
cnt[i].els++;
break;
}
if(a[i]%2==0)
{
a[i]/=2;
cnt[i].tw++;
}
if(a[i]%3==0)
{
a[i]/=3;
cnt[i].thr++;
}
}
}
for(int i=1;i<n;i++)
{
if(cnt[i].els!=cnt[i+1].els)
{
printf("-1");
return 0;
}
}
int common2=1e9,common3=1e9;
for(int i=1;i<=n;i++)
{
common2=min(common2,cnt[i].tw);
common3=min(common3,cnt[i].thr);
}
int ans=0;
for(int i=1;i<=n;i++)
{
ans+=cnt[i].tw-common2;
ans+=cnt[i].thr-common3;
}
printf("%d",ans);
return 0;
}