题解:[Algo Beat 009 & MROI R1] Common Ratio
ShootingStar324 · · 题解
这篇题解并没有ムラサメ的那一篇详细也没有那篇好懂 qwq,不过如果你需要码风比较清晰的 std 可以看这里喵!
当 0<n\leq 2 时
此时直接输出 0,因为容易证明此时无需进行任何修改。
当 2<n\leq 64 时
此时
在 因本人语文能力有限使用伪代码进行演示。
:::info[伪代码]
for k1 from i-1 to 1:
if a[k1]*r0!=a[k1+1]:
if(a[k1+1]/r0 notin Z):
invalid
else:
a[k1]<-a[k1+1]/r0
cnt<-cnt+1
for k2 from i+1 to j-1:
if a[k2-1]*r0!=a[k2]:
if(a[k2-1]*r0 notin Z):
invalid
else:
a[k2]<-a[k2-1]*r0
cnt<-cnt+1
for k3 from j+1 to n:
if a[k3-1]*r0!=a[k3]:
if(a[k3-1]*r0 notin Z):
invalid
else:
a[k3]<-a[k3-1]*r0
cnt<-cnt+1
| (我不会写伪代码,各位看得懂就行) |
|---|
这一部分时间复杂度
当 n>64 时
此时可以证明,合法等比数列的公比只能是
:::info[Code]
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef __int128_t i128;
#define PLL pair<ll,ll>
const ll N=2e5+10;
const i128 INT128_MAX=170141183460469231731687303715884105727;
const i128 INT128_MIN=-170141183460469231731687303715884105728;
ll t;
ll n;
ll a[N];
ll tmpa[N];
struct frac{
i128 x;
i128 y;
void reduce(){
if(x==0){
y=1;
return;
}
i128 gcd=__gcd(x<0?-x:x,y);
x/=gcd;
y/=gcd;
}
frac(i128 a=0,i128 b=1):x(a),y(b){
if(b<0){
x=-x;
y=-y;
}
reduce();
}
frac operator*(frac other)const{
frac tmp=frac(x*other.x,y*other.y);
tmp.reduce();
return tmp;
}
frac operator/(frac other)const{
frac tmp=frac(x*other.y,y*other.x);
tmp.reduce();
return tmp;
}
bool operator==(frac other)const{
return x==other.x && y==other.y;
}
bool operator!=(frac other)const{
return !(*this==other);
}
bool operator<(frac other)const{
return x*other.y<other.x*y;
}
bool operator>(frac other)const{
return other<*this;
}
bool operator<=(frac other)const{
return !(other<*this);
}
bool operator>=(frac other)const{
return !(*this<other);
}
};
pair<bool,i128> root(i128 x,i128 k){
i128 l=1,r=x;
while(l<=r){
i128 mid=(l+r)/2;
i128 tmp=1;
for(ll i=1;i<=k;i++){
if(tmp<0){
if(tmp<INT128_MAX/mid || tmp>INT128_MIN/mid){
return {false,0};
}
}
else{
if(tmp>INT128_MAX/mid || tmp<INT128_MIN/mid){
tmp=INT128_MAX; break;
}
}
tmp*=mid;
}
if(tmp==x){
return {true,mid};
}
else if(tmp<x){
l=mid+1;
}
else{
r=mid-1;
}
}
return {false,0};
}
bool rootf(frac &x,i128 k){
frac y=x;
y.reduce();
if(y.x<0){
if(k%2==0){
return false;
}
frac pos=y;
pos.x=-pos.x;
if(rootf(pos,k)){
x=frac(-pos.x,pos.y);
return true;
}
return false;
}
PLL resx=root(y.x,k),resy=root(y.y,k);
if(resx.first && resy.first){
x=frac(resx.second,resy.second);
return true;
}
else{
return false;
}
}
bool check(frac f){
return (f>=frac(LONG_LONG_MIN,1) && f<=frac(LONG_LONG_MAX,1));
}
ll cal(ll i,ll j,frac r0){
memcpy(tmpa,a,(n+1)*sizeof(ll));
ll cnt=0;
for(ll k=i-1;k>=1;k--){
if(frac(tmpa[k+1],tmpa[k])!=r0){
cnt++;
frac tmp=frac(tmpa[k+1],1)/r0;
if(!check(tmp)){
return 1e18;
}
if(tmp.y!=1){
return 1e18;
}
else{
tmpa[k]=tmp.x;
}
}
}
for(ll k=i+1;k<j;k++){
if(frac(tmpa[k],tmpa[k-1])!=r0){
cnt++;
frac tmp=frac(tmpa[k-1],1)*r0;
if(!check(tmp)){
return 1e18;
}
if(tmp.y!=1){
return 1e18;
}
else{
tmpa[k]=tmp.x;
}
}
}
for(ll k=j+1;k<=n;k++){
if(frac(tmpa[k],tmpa[k-1])!=r0){
cnt++;
frac tmp=frac(tmpa[k-1],1)*r0;
if(!check(tmp)){
return 1e18;
}
if(tmp.y!=1){
return 1e18;
}
else{
tmpa[k]=tmp.x;
}
}
}
return cnt;
}
ll cal2(){
map<ll,ll> mp1,mp2,mp3;
for(ll i=1;i<=n;i++){
mp1[a[i]]++;
if(i%2){
mp2[a[i]]++;
mp3[-a[i]]++;
}
else{
mp2[-a[i]]++;
mp3[a[i]]++;
}
}
ll res=1e18;
for(auto it:mp1){
res=min(res,n-it.second);
}
for(auto it:mp2){
res=min(res,n-it.second);
}
for(auto it:mp3){
res=min(res,n-it.second);
}
return res;
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>t;
while(t--){
cin>>n;
for(ll i=1;i<=n;i++){
cin>>a[i];
}
if(n==1){
cout<<"0\n";
continue;
}
ll ans=1e18;
if(n<=64){
for(ll i=1;i<=n;i++){
for(ll j=i+1;j<=n;j++){
frac r0(a[j],a[i]);
if(!rootf(r0,j-i)){
continue;
}
ans=min(ans,cal(i,j,r0));
}
}
}
ans=min(ans,cal2());
cout<<ans<<"\n";
}
return 0;
}
:::