Codeforces 1255E Send Boxes to Alice (prefix and number theory + + enum)

We consider prefix and sum [i], if a [i + 1] in a stuffing a [i], the not affected sum [i + 1], but the sum [i] ++, if a [ a i] is inserted into a [i + 1], will not affect the sum [i + 1], but the sum [i] -, we can see that once the operation is equivalent to a sum [i] + 1'd or the sum [i] -1, then the question becomes is intended to be many times the operation such that all of the sum [i] k is a multiple of one, then a sum [i] becomes a multiple of the operating frequency and k is the inevitable minimum min (sum [i]% k, k- sum [i]% k), then took what it considered k, k is obviously sum [n] is a factor, this would certainly impossible, but the sum [n] range 1e12, enumerate All factors of up to 2 12 Ge, obviously not, but in fact we only need to enumerate the prime factors, it is concluded his prime factors of the number of operating income does not exceed the minimum the inevitable operating income of k. The overall complexity of O (12n).

 1 //      ——By DD_BOND
 2 
 3 #include<bits/stdc++.h>
 4 
 5 #define pb push_back
 6 
 7 using namespace std;
 8 
 9 typedef long long ll;
10 
11 const ll LLMAX=2e18;
12 const int MAXN=1e6+10;
13 
14 vector<ll>prime;
15 ll a[MAXN],sum[MAXN];
16 
17 int main(void)
18 {
19     ios::sync_with_stdio(false);    cin.tie(0);   cout.tie(0);
20     ll n,ans=LLMAX;  cin>>n;
21     for(int i=1;i<=n;i++)    cin>>a[i],sum[i]=sum[i-1]+a[i];
22     if(sum[n]==0)   return cout<<0<<endl,0;
23     if(sum[n]==1)   return cout<<-1<<endl,0;
24     ll p=sum[n];
25     for(ll i=2;i*i<=sum[n];i++)
26         while(p%i==0){
27             prime.pb(i);
28             p/=i;
29         }
30     if(p!=1) prime.pb(p);
31     prime.erase(unique(prime.begin(),prime.end()),prime.end());
32     for(auto i:prime){
33         ll cur=0;
34         for(int j=1;j<=n;j++)    cur+=min(sum[j]%i,i-sum[j]%i);
35         ans=min (years, cur);
36      }
 37      cout << age << endl;
38      return  0 ;
39 }

Guess you like

Origin www.cnblogs.com/dd-bond/p/11918701.html