Array GCD CodeForces - 624D (數學,gcd)
阿新 • • 發佈:2019-04-30
stream bits can 暴力枚舉 || vector num style 簡單
大意: 給定序列, 給定常數a,b, 兩種操作, (1)任選一個長為$t$的子區間刪除(不能全部刪除), 花費t*a. (2)任選$t$個元素+1/-1, 花費t*b. 求使整個序列gcd>1的最少花費.
題目有個限制是不能全部刪除, 所以最後一定剩余a[1]或a[n], 暴力枚舉a[1]與a[n]的所有素因子即可.
這場div. 2題目感覺都挺簡單的, 但實現起來各種出錯...........各種細節還是沒考慮好......
#include <iostream> #include <iostream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #include <map> #include <queue> #include <string> #include <string.h> #include <bitset> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl ‘\n‘ #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<‘ ‘;hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} inline int rd() {int x=0;char p=getchar();while(p<‘0‘||p>‘9‘)p=getchar();while(p>=‘0‘&&p<=‘9‘)x=x*10+p-‘0‘,p=getchar();return x;} //head const int N = 1e6+10; int n, x, y; int a[N], b[N]; ll ans; ll solve(int g, int *a, int n) { int L=n+1,R=0,toty=0,totx=0; REP(i,0,n) b[i]=0; REP(i,1,n) if (a[i]%g) { if ((a[i]-1)%g&&(a[i]+1)%g) { L = min(L,i); R = i; ++totx; } b[i]=1; } REP(i,L,R) b[i]=0; REP(i,1,n) toty+=b[i]; totx = R-L+1; ll ans = (ll)totx*x+(ll)toty*y; if (L==n+1) { ans = (ll)toty*y; b[0] = 0; REP(i,1,n) b[i]+=b[i-1]; ll mi = 0; REP(i,1,n) { ans = min(ans, (ll)toty*y+(ll)i*x-(ll)b[i]*y+mi); mi = min(mi, -(ll)i*x+(ll)b[i]*y); } return ans; } int dx = 0, dy = 0; ll d = 0; PER(i,1,L-1) { ++dx; if (b[i]) --dy; d = min(d, (ll)dx*x+(ll)dy*y); } ans += d, d = 0, dx = dy = 0; REP(i,R+1,n) { ++dx; if (b[i]) --dy; d = min(d, (ll)dx*x+(ll)dy*y); } ans += d; return ans; } vector<int> fac(int num) { int mx = sqrt(num+0.5); vector<int> v; REP(i,2,mx) if (num%i==0) { v.pb(i); while (num%i==0) num/=i; } if (num>1) v.pb(num); return v; } void solve(int num, int *a, int n, int tp) { vector<int> g = fac(num); for (auto &&t:g) ans = min(ans, solve(t,a,n)+tp*y); } int main() { scanf("%d%d%d", &n, &x, &y); REP(i,1,n) scanf("%d", a+i); ans = 1e15; solve(a[1],a+1,n-1,0); solve(a[1]-1,a+1,n-1,1); solve(a[1]+1,a+1,n-1,1); solve(a[n],a,n-1,0); solve(a[n]-1,a,n-1,1); solve(a[n]+1,a,n-1,1); printf("%lld\n", ans); }
Array GCD CodeForces - 624D (數學,gcd)