SHARE
TWEET

Untitled

a guest Oct 18th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int expresie(int &coefx, int &coefy, int &val);
  4. int termen(int &coefx, int &coefy, int &val);
  5. int factor(int &coefx, int &coefy, int &val);
  6. char s[502],s0[502]; int p=-1;
  7. bool ok[5000001];
  8. int expresie(int &coefx, int &coefy, int &val){ int auxcoefx=0, auxcoefy=0, auxval=0; char semn;
  9. p++;
  10. termen(auxcoefx, auxcoefy, auxval);
  11. coefx=auxcoefx; coefy=auxcoefy; val=auxval;
  12. while(s[p]=='+' || s[p]=='-'){semn=s[p]; p++; termen(auxcoefx,auxcoefy,auxval);
  13.  if(semn=='+'){ coefx+=auxcoefx; coefy+=auxcoefy; val+=auxval;}
  14.  else {coefx-=auxcoefx; coefy-=auxcoefy; val-=auxval;}
  15. }
  16. }
  17.  
  18. int termen(int &coefx, int &coefy, int &val){ int auxcoefx=0, auxcoefy=0, auxval=0;
  19. //aici inmultim;
  20. if(s[p]=='x'){auxcoefx=1; auxcoefy=0; auxval=0; p++;}
  21.   else if(s[p]=='y'){auxcoefx=0; auxcoefy=1; auxval=0; p++;}
  22.   else factor(auxcoefx,auxcoefy,auxval);
  23.   coefx=auxcoefx; coefy=auxcoefy; val=auxval;
  24. while(s[p]=='*'){
  25.     p++; if(s[p]=='x'){auxcoefx=1; auxcoefy=0; auxval=0; p++;}
  26. else if(s[p]=='y'){auxcoefx=0; auxcoefy=1; auxval=0; p++;}
  27. else factor(auxcoefx,auxcoefy,auxval);
  28. //separare pe cazuri: avem inmultiri de forma (Ax+B)*valoare, valoare*(Ax+B) si analoage ptr y; sau pur si simplu doua numere naturale inmultite
  29. if(coefx!=0)coefx*=auxval;
  30. else if(auxcoefx!=0)coefx=auxcoefx*val;
  31. if(coefy!=0)coefy*=auxval;
  32. else if(auxcoefy!=0)coefy=auxcoefy*val;
  33. val*=auxval;
  34. }
  35. }
  36.  
  37. int factor(int &coefx, int &coefy, int &val){ int auxcoefx=0, auxcoefy=0, auxval=0;
  38. if(s[p]=='('){expresie(auxcoefx, auxcoefy, auxval); coefx=auxcoefx; coefy=auxcoefy; val=auxval; p++;}
  39. else{
  40.         int nr=0;
  41.         while(isdigit(s[p])){nr=nr*10+(s[p]-'0'); p++;}
  42.         val=nr;
  43. }
  44. }
  45.  
  46. int main()
  47. {
  48.     int coefx,coefy,val,meh1,meh2,val2,i=0,j,len0,len,poz,a,b,c,deverificat,nrsol=0,oky=0,okx,x,y; meh1=meh2=val2=coefx=coefy=val=0;
  49.     cin.getline(s0,257);
  50.     //separare Membru stang
  51.     while(s0[i]!='='){s[i]=s0[i]; i++;}
  52. //    cout<<s<<" ";
  53.     poz=++i; len=strlen(s);
  54.     //daca apare o structura de forma 12x, sau (-5)y, introducem "*" pentru a ne usura munca mai apoi
  55.        //in plus, marchez daca apare y in expresie
  56.     for(i=1;i<len;i++)
  57.         if(strchr("xy",s[i])){
  58.             if(isdigit(s[i-1]) || s[i-1]==')'){
  59.               for(j=len-1;j>=i;j--)
  60.                  s[j+1]=s[j];
  61.               s[i]='*';
  62.               len++;
  63.             }
  64.             if(s[i]=='y')oky=1;
  65.         }
  66.     expresie(coefx,coefy,val);
  67.     //resetare s;
  68.     for(i=0;i<len;i++)s[i]=0; p=-1; i=0;
  69.     //reluare proces membru drept, de data fiind mai simplu
  70.     while(s0[poz]){s[i]=s0[poz]; poz++; i++;} len=strlen(s);
  71.     expresie(meh1,meh2,val2);
  72.     //momentam avem coefx*x+coefy*y+val=val2, trecem in ec. de forma  coefx*x+coefy*y+val=val, sau a*x+b*y=c;
  73.     c=val2-val; b=coefy; a=coefx;
  74. //    cout<<coefx<<" "<<coefy<<" "<<val<<" "<<val2;
  75. //    cout<<'\n'<<a<<" "<<b<<" "<<c<<"   "<<oky<<'\n';
  76. //    migalosenii: verificam daca coeficientii lui x sau/si lui y sunt 0;
  77.     if(!oky || b==0){
  78.         if(a!=0){
  79.         if((c%a)==0){ deverificat=c/a;
  80.             if(deverificat<2)cout<<"No solution";
  81.             for(i=2,okx=1;i*i<=deverificat;i++)if(deverificat%i==0){cout<<"No solution"; okx=0; break;}
  82.             if(okx){cout<<"x="<<deverificat; if(oky)cout<<",y=2";}
  83.         }
  84.         else cout<<"No solution";
  85.         }
  86.         //daca coeficientul lui a este 0
  87.         else {if(c==0 && !oky)cout<<"x=2";
  88.         else if(c==0 && oky)cout<<"x=2,y=2";
  89.         else cout<<"No solution";
  90.         }
  91.     }
  92.     else if(oky && b!=0){
  93.          //ciur! + nu mai verificam numerele divizibile cu 2, pentru a reduce la 5*10^6/2 numarul de posibilitati
  94.     for(i=3;i*i<=5000000;i+=2)
  95.         if(!ok[i])
  96.             for(j=i*i;j<=5000000;j+=i)ok[j]=1;
  97.     //luam fiecare x prim si verificam daca y=(c-a*x)/b e si el prim; sa nu uitam sa verificam daca y e par(!)
  98.     //x=2
  99.     if((c-2*a)%b==0 && (c-a*x)/b >=2){y=(c-2*a)/b; if(y==2 || (ok[y]==0 && y%2==1)){cout<<"x="<<2<<",y="<<y; nrsol=1;}   }
  100.     //x prim, >2
  101.     for(x=3;x<=5000000 && !nrsol;x+=2)
  102.         if(!ok[x])
  103.           if((c-a*x)%b == 0 && (c-a*x)/b >= 2) {
  104.            y=(c-a*x)/b; if(y==2 || (ok[y]==0 && y%2==1)){cout<<"x="<<x<<",y="<<y; nrsol=1;}
  105.           }
  106.        if(!nrsol)cout<<"No solution";
  107.     }
  108.     return 0;
  109. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top