Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N=5000001;
- bool ciur[N];
- vector<unsigned int>prim;
- bool ok,X,Y;
- struct ecuatie
- {
- long long val,coefx,coefy;
- ecuatie operator+ (const ecuatie &rhs) const
- {
- return {val+rhs.val,coefx+rhs.coefx,coefy+rhs.coefy};
- }
- ecuatie operator- (const ecuatie &rhs) const
- {
- return {val-rhs.val,coefx-rhs.coefx,coefy-rhs.coefy};
- }
- ecuatie operator* (const ecuatie &rhs) const
- {
- if(coefx==0&&coefy==0)
- return {val*rhs.val,rhs.coefx*val,rhs.coefy*val};
- else
- return {val*rhs.val,coefx*rhs.val,coefy*rhs.val};
- }
- };
- void Ciur()
- {
- ciur[0]=ciur[1]=1;
- for(int i=4;i<=N;i+=2)
- ciur[i]=1;
- for(int i=3;i*i<=N;++i)
- if(ciur[i]==0)
- for(int j=i*i;j<=N;j+=2*i)
- ciur[j]=1;
- for(int i=2;i<=N;++i)
- if(!ciur[i])
- prim.push_back(i);
- }
- unsigned int poz=0;
- ecuatie expresie(string &s);
- ecuatie numar(string &s)
- {
- unsigned long long nr=0;
- while(poz<s.size()&&isdigit(s[poz]))
- {
- nr=nr*10+s[poz]-'0';
- poz++;
- }
- return {(long long) nr,0,0};
- }
- ecuatie factor(string &s) ///4x=16
- {
- ecuatie nr;
- nr={0,0,0};
- if(s[poz]=='x')
- {
- nr.coefx=1;
- poz++;
- }
- else
- if(s[poz]=='y')
- {
- nr.coefy=1;
- poz++;
- }
- else
- if(s[poz]=='(')
- {
- poz++;
- nr=expresie(s);
- poz++;
- }
- else
- {
- nr=numar(s);
- }
- return nr;
- }
- ecuatie termen(string &s)
- {
- ecuatie nr=factor(s);
- while(poz<s.size()&&s[poz]=='*')
- {
- poz++;
- if(s[poz-1]=='*')
- nr=nr*factor(s);
- }
- return nr;
- }
- ecuatie expresie(string &s)
- {
- ecuatie nr=termen(s);
- while(poz<s.size()&&(s[poz]=='+'||s[poz]=='-'))
- {
- poz++;
- if(s[poz-1]=='+')
- nr=nr+termen(s);
- else
- nr=nr-termen(s);
- }
- return nr;
- }
- int main()
- {
- string aux,s,s1;
- Ciur();
- cin>>aux;
- for(unsigned int i=0;i<aux.size();++i)
- {
- if(aux[i]=='x')
- X=1;
- if(aux[i]=='y')
- Y=1;
- if(aux[i]=='=')
- {
- ok=1;
- continue;
- }
- if(i<aux.size()-1&&isdigit(aux[i])&&isalpha(aux[i+1]))
- {
- if(!ok)
- s+=aux[i],s+='*';
- if(ok)
- s1+=aux[i],s1+='*';
- }
- else
- {
- if(!ok)
- s+=aux[i];
- else
- s1+=aux[i];
- }
- }
- ecuatie k=expresie(s);
- s=s1;
- poz=0;
- ecuatie k1=expresie(s);
- k1.val-=k.val;
- if(Y==0)
- {
- if(k.coefx==0&&k1.val==0)
- {
- cout<<"x=2";
- return 0;
- }
- if(k.coefx==0)
- {
- cout<<"No solution";
- return 0;
- }
- if(k1.val%k.coefx==0&&k1.val/k.coefx>=0&&!ciur[k1.val/k.coefx])
- cout<<"x="<<k1.val/k.coefx;
- else
- cout<<"No solution";
- }
- else
- {
- if(k.coefx==0&&k.coefy==0&&k1.val==0)
- {
- cout<< "x=2,y=2";
- return 0;
- }
- if(k.coefx==0)
- {
- cout<<"x=2"<<","<<"y="<<k1.val/k.coefy;
- return 0;
- }
- if(k.coefy==0)
- {
- if(k1.val%k.coefx==0)
- cout<<"x="<<k1.val/k.coefx; // aici e schimbarea
- else
- cout<<"x="<<k1.val/k.coefx<<','<<"y=2";
- return 0;
- }
- for(unsigned int i=0;i<prim.size()&&prim[i]<=k1.val;++i)
- {
- long long t=1ll*prim[i]*k.coefx;
- if((k1.val-t)%k.coefy==0&&(k1.val-t)/k.coefy>=0&&!ciur[(k1.val-t)/k.coefy])
- {
- cout<<"x="<<prim[i]<<','<<"y="<<(k1.val-t)/k.coefy;
- return 0;
- }
- }
- cout<<"No solution";
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment