/ Vijos / 讨论 / 编程 /

缺省源

inline int read()
{
    int s=0,w=1;char ch=getchar();
    while(ch<'0'||ch>'9'){ if(ch=='-') w=-1;ch=getchar();}//如果输入的是换行符、负号……需要判一下!
    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();//输入数字部分
    return s*w;//返回值:符号(+,-) × 数的绝对值
}

1 条评论

  • @ 2018-11-10 12:57:57
    #include<cstdio>
    #include<math.h>
    #include<string>
    #include<string.h>
    #include<iostream>
    using namespace std;
    int compare(string str1,string str2)
    {
        if(str1.length()>str2.length()) return 1;
        else if(str1.length()<str2.length())  return -1;
        else return str1.compare(str2);
    }
    
    string addd(string str1,string str2)
    {
        string str;
        int len1=str1.length();
        int len2=str2.length();
    
        if(len1<len2)
        {
            for(int i=1;i<=len2-len1;i++)
               str1="0"+str1;
        }
        else
        {
            for(int i=1;i<=len1-len2;i++)
               str2="0"+str2;
        }
        len1=str1.length();
        int cf=0;
        int temp;
        for(int i=len1-1;i>=0;i--)
        {
            temp=str1[i]-'0'+str2[i]-'0'+cf;
            cf=temp/10;
            temp%=10;
            str=char(temp+'0')+str;
        }
        if(cf!=0)  str=char(cf+'0')+str;
        return str;
    }
    
    string subb(string str1,string str2)
    {
        string str;
        int tmp=str1.length()-str2.length();
        int cf=0;
        for(int i=str2.length()-1;i>=0;i--)
        {
            if(str1[tmp+i]<str2[i]+cf)
            {
                str=char(str1[tmp+i]-str2[i]-cf+'0'+10)+str;
                cf=1;
            }
            else
            {
                str=char(str1[tmp+i]-str2[i]-cf+'0')+str;
                cf=0;
            }
        }
        for(int i=tmp-1;i>=0;i--)
        {
            if(str1[i]-cf>='0')
            {
                str=char(str1[i]-cf)+str;
                cf=0;
            }
            else
            {
                str=char(str1[i]-cf+10)+str;
                cf=1;
            }
        }
        str.erase(0,str.find_first_not_of('0'));
        return str;
    }
    
    string mull(string str1,string str2)
    {
        string str;
        int len1=str1.length();
        int len2=str2.length();
        string tempstr;
        for(int i=len2-1;i>=0;i--)
        {
            tempstr="";
            int temp=str2[i]-'0';
            int t=0;
            int cf=0;
            if(temp!=0)
            {
                for(int j=1;j<=len2-1-i;j++)
                  tempstr+="0";
                for(int j=len1-1;j>=0;j--)
                {
                    t=(temp*(str1[j]-'0')+cf)%10;
                    cf=(temp*(str1[j]-'0')+cf)/10;
                    tempstr=char(t+'0')+tempstr;
                }
                if(cf!=0) tempstr=char(cf+'0')+tempstr;
            }
            str=addd(str,tempstr);
        }
        str.erase(0,str.find_first_not_of('0'));
        return str;
    }
    void _divv(string str1,string str2,string &quotient,string &residue)
    {
        quotient=residue="";
        if(str2=="0")
        {
            quotient=residue="ERROR";
            return;
        }
        if(str1=="0")
        {
            quotient=residue="0";
            return;
        }
        int res=compare(str1,str2);
        if(res<0)
        {
            quotient="0";
            residue=str1;
            return;
        }
        else if(res==0)
        {
            quotient="1";
            residue="0";
            return;
        }
        else
        {
            int len1=str1.length();
            int len2=str2.length();
            string tempstr;
            tempstr.append(str1,0,len2-1);
            for(int i=len2-1;i<len1;i++)
            {
                tempstr=tempstr+str1[i];
                tempstr.erase(0,tempstr.find_first_not_of('0'));
                if(tempstr.empty())
                  tempstr="0";
                for(char ch='9';ch>='0';ch--)
                {
                    string str,tmp;
                    str=str+ch;
                    tmp=mull(str2,str);
                    if(compare(tmp,tempstr)<=0)
                    {
                        quotient=quotient+ch;
                        tempstr=subb(tempstr,tmp);
                        break;
                    }
                }
            }
            residue=tempstr;
        }
        quotient.erase(0,quotient.find_first_not_of('0'));
        if(quotient.empty()) quotient="0";
    }
    string modd(string a,string b)
    {
        string tmp1,tmp2;
        _divv(a,b,tmp1,tmp2);
        return tmp2;
    }
    string divv(string a,string b)
    {
        string tmp1,tmp2;
        _divv(a,b,tmp1,tmp2);
        return tmp1;
    }
    string b(string n,string jz)
    {
        string result="0",k="1",i,tmp;
        tmp=n;
        while(tmp!="0")
        {
            i=modd(tmp,jz);
            result=addd(mull(k,i),result);
            k=mull(k,"10");
            tmp=divv(tmp,jz);
        }
        return result;
    }
    bool p(string a)
    {
        int i,length,flag=1;
        length=a.length();
        for(i=0;i<=length/2;i++) 
        {
           if(a[i]!=a[length-i-1]) {
               flag=0;
               break;
           }
        }
        return flag;
    }
    string pow(string a)
    {
        string aa="0",ans="1";
        while(aa!=a)
        {
            ans=mull(ans,"2");
            aa=addd(aa,"1");
        }
        return ans;
    }
    int main()
    {
        string a;
        cin>>a;
        //cout<<pow(a)<<endl;
        cout<<subb(pow(a),addd(addd("1",a),divv(mull(a,subb(a,"1")),"2")));
    }
    
  • 1