复制代码

Codeforces Round 491

全场暴力 挺毒的

A

数字问题很简单

#include <bits/stdc++.h>
using namespace std;
int main()
{
    freopen("in","r",stdin);\
         freopen("out","w",stdout); 

    int a,b,c,n;
    cin>>a>>b>>c>>n;
    if(a<c||b<c){
        cout<<-1<<endl;
        return 0 ;
    }
    c= a+b-c;
    if(c+1>n){
        cout<<-1<<endl;
        return 0;
    }
    cout<<n-c<<endl;
    return 0;
}
View Code

 

B
暴力模拟
求和

#include<bits/stdc++.h>
using namespace std;
#define LOACL  freopen("in","r",stdin);\
         freopen("out","w",stdout); 


#define DBG(x) cout<<(#x)<<"="<<x<<endl
#define DBG2(x,y) cout<<(#x)<<"="<<x<<"\t"<<(#y)<<"="<<y<<endl
#define DBG3(x,y,z) cout<<(#x)<<"="<<x<<"\t"<<(#y)<<"="<<y<<"\t"<<(#z)<<"="<<z<<endl
 
int a[6],n ,tmp;  
double sum;
int main()
{
    LOACL
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>tmp;
        a[tmp]++;
        sum+=tmp;
    }
    int ans = 0; 
     sum = 4.5*n - sum;
    int t = 2;
   
    while(sum>=0&&t<=4)
    {
        if(sum>(5-t)*a[t])
        {
            sum-=(5-t)*a[t];      
            ans+=a[t]; 
            t++; 
        }
        else
        {
            /*cout<<"aaaaaaaaaaaaaaa"<<endl; 
              cout<<sum<<t<<endl;     
            cout<< (int)sum/(5-t)*(5-t)<<endl;
            cout<< sum/(5-t)<<endl;
            */
   
            if((int)sum/(5-t)*(5-t) < sum)ans++;  
            ans+=(int)sum/(5-t);
            break;
        }
    }
   cout<<ans<<endl;
    
}
View Code

 

C

二分就好,我的二分有毒

#include<bits/stdc++.h>
using namespace std;
#define LOACL  freopen("in","r",stdin);\
         freopen("out","w",stdout); 
#define DBG(x) cout<<(#x)<<"="<<x<<endl
#define DBG2(x,y) cout<<(#x)<<"="<<x<<"\t"<<(#y)<<"="<<y<<endl
#define DBG3(x,y,z) cout<<(#x)<<"="<<x<<"\t"<<(#y)<<"="<<y<<"\t"<<(#z)<<"="<<z<<endl
 
typedef long long ll;
bool calc(ll r,ll t)
{
    ll a=0 ,n=t;
    while(n)
    {
        if(r>n){a+=n;n=0;}
        else 
        {
            a+=r;
            n-=r; 
            n-=ll (n/10);    
        }    
    }
    //DBG2(a,b);
    return a*2>=t;
}
int main()
{    
    LOACL
    ll n,l,r,mid;
    cin>>n;
    l=1,r=n;  

    while(l<r)
    {
        mid = (l+r)/2;
        if(calc(mid,n))
            r=mid;
        else l=mid+1;
    } 
    cout<<l<<endl; 
}
View Code

 

D
反向搜索,鬼记搜

#include<bits/stdc++.h>
using namespace std;
#define LOACL  freopen("in","r",stdin);\
         freopen("out","w",stdout);  
#define DBG(x) cout<<(#x)<<"="<<x<<endl
#define DBG2(x,y) cout<<(#x)<<"="<<x<<"\t"<<(#y)<<"="<<y<<endl
#define DBG3(x,y,z) cout<<(#x)<<"="<<x<<"\t"<<(#y)<<"="<<y<<"\t"<<(#z)<<"="<<z<<endl
 
char a[2][105];
int check(int pos)
{
    int ans= 0;
    if(a[0][pos] == '0')
        ans++;
    if(a[1][pos] == '0')
        ans++; 
     return ans;
} 
int main()
{
    LOACL
    cin>>a[0]>>a[1];
    int n = strlen(a[1]);
    int ans=0,ob =0;
   // DBG(n-1);
   // DBG(check(0));
    #if 1
    for(int i=n-1;i>=0;i--)
    {
        int ret = check(i);
        if(ret+ob >=3 )
        {
            ans++;
            ob = ret+ob-3;
        }else
        {
            ob =ret;
        }
    }
    #endif
    cout<<ans<<endl;
}
View Code

 

E

全搜  + 常数优化 

排队问题

 #include<bits/stdc++.h>
using namespace std;
#define LOACL  freopen("in","r",stdin);\
         freopen("out","w",stdout);  
#define DBG(x) cout<<(#x)<<"="<<x<<endl
#define DBG2(x,y) cout<<(#x)<<"="<<x<<"\t"<<(#y)<<"="<<y<<endl
#define DBG3(x,y,z) cout<<(#x)<<"="<<x<<"\t"<<(#y)<<"="<<y<<"\t"<<(#z)<<"="<<z<<endl
typedef long long ll;

ll fact[22];
int cnt[10];
string s;
ll dfs(vector<ll> v,int sum)
{
    ll ret =0;
    ll reta = 0;
    if(v.size()==10)
    {
        ret = fact[sum];
        for(int i=1;i<v.size();i++) ret/=fact[v[i]];
        reta=fact[sum-1+v[0]];
        reta/=fact[sum-1];
        reta/=fact[v[0]];
        return ret*reta;
    }
    ll len = v.size();
    v.push_back(0);
    if(cnt[len]==0)return dfs(v,sum);
    for (int i = 1; i <= cnt[len]; ++i)
    {
        v[len]=i;
        if(len==0) ret+=dfs(v,sum);
        else ret+=dfs(v,sum+i);
    }
    return ret;
} 
int main()
{
    LOACL
    fact[0]=1;
    for(int i=1;i<=20;i++)fact[i]=fact[i-1]*i;
    cin>>s;
    int n=s.length();
    for(int i=0;i<n;i++) cnt[s[i]-'0']++;
    vector<ll> v;
    cout<<dfs(v,0)<<endl;    
}
View Code

 

F

暴力模拟
分类求最优

#include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
#define local freopen("in","r",stdin);\
            freopen("out","w",stdout);
#define DBG(x) cout<<(#x)<<"="<<x<<endl
#define DBG2(x,y) cout<<(#x)<<"="<<x<<"\t"<<(#y)<<"="<<y<<endl
#define DBG3(x,y,z) cout<<(#x)<<"="<<x<<"\t"<<(#y)<<"="<<y<<"\t"<<(#z)<<"="<<z<<endl
ll n ;
string best;
 
int get_length(ll n )
{
    int count = 0;
    do{
        count++;
        n/=10;
    }while(n);
    return count;
}
string lltoString(ll n)
{
    string str;
    while(n)
    {
        char c= n-n/10*10+'0';
        str =str+c;
        n/=10;
    }
    reverse(str.begin(),str.end());
    return str;
} 
ll pow(ll a,ll b)
{
    ll v = 1 ;
    while(b)
    {
        if(b&1)
        {
            v=v*a;    
        }
        a=a*a;
        b/=2;
    }
    return v;
}
int main()
{
    local;
    cin>>n;
    best = lltoString(n);
     
    // a^b *c+d
    // 
    for(ll b = 2; pow(2, b) <= 1e18; b++)
         for(ll a = 2;pow(a,b)<=1e18&& pow(a,b)<=n;a++)
         {
        
             ll c = n/pow(a,b);
             ll d = n-c*pow(a,b);
         //     DBG2(c,d);
     #if 1 
             string cacdidate = lltoString(a)+"^"+lltoString(b);
             if(c>1)
                 cacdidate+="*"+lltoString(c);
             if(d>0)
                 cacdidate+="+"+lltoString(d); 
             
             if(cacdidate.size()<best.size())
                 best = cacdidate;
    #endif
         }
//    DBG(best);

    // a^b + c^d*e + f
    // 
    for(ll b =2;pow(2,b)<=1e18;b++)
        for(ll a =2 ;a<=1000&&pow(a,b)<=min((ll)1e18,n);a++)
        {
            ll p = pow(a,b);
            for(ll d =2 ; p+pow(2,d)<=n;d++)
                for(ll c =2;c< (a>=100?10:100) && p+pow(c,d)<=min((ll)1e18,n) ; c++ )
                {
                    ll p2 = pow(c,d);
                    ll e= (n-p)/p2;
                    ll f= n-p-p2*e;
                    int length = get_length(a)+get_length(b)+get_length(c)+get_length(d)+3;
                    if(length >= best.size()) continue;
                    if(e>1)
                        length+=get_length(e)+1;
                    if(f>0)
                         length+=get_length(f)+1;
                     if(length<best.size())
                     {
                         string cacdidate = lltoString(a)+"^"+lltoString(b)+"+"+lltoString(c)+"^"+lltoString(d);
                        if(e>1)
                            cacdidate+="*"+lltoString(e);
                        if(f>0)
                            cacdidate+="+"+lltoString(f);
                        if(cacdidate.size()<best.size())
                            best = cacdidate;
                     } 
                }
        }
    
    //a^b*c^d*e +f

    for(ll b =2;pow(2,b)<=1e18;b++)
        for(ll a =2 ;a<=1000&&pow(a,b)<=min((ll)1e18,n);a++)
        {
            ll  p= pow(a,b);
            for(ll d =2 ; p*pow(2,d)<=n;d++)
                for(ll c =2;c< (a>=100?10:100) && p*pow(c,d)<=min((ll)1e18,n) ; c++ )
                {
                    ll pp = p*pow(c,d);
                    ll e = n/pp;
                    ll f = n-pp*e;
                    int length = get_length(a)+get_length(b)+get_length(c)+get_length(d)+3;

                    if(length>best.size()) continue;
                    if(e>1) length+=get_length(e)+1;
                    if(f>0) length+=get_length(f)+1;
                    if(length<best.size())
                    {
                        string cacdidate=  lltoString(a)+"^"+lltoString(b)+"*"+lltoString(c)+"^"+lltoString(d);
                        if(e>1)
                            cacdidate+="*"+lltoString(e);
                        if(f>0)
                            cacdidate+="+"+lltoString(f);
                        if(cacdidate.size()<best.size())
                            best = cacdidate;
                    }

                }
        }
    cout<<best<<endl;
}
View Code

 

posted @ 2018-06-24 20:11  pg633  阅读(112)  评论(0编辑  收藏  举报