a53

ecuatie1

a53
Apr 19th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N=5000001;
  4. bool ciur[N];
  5. vector<unsigned int>prim;
  6. bool ok,X,Y;
  7.  
  8. struct ecuatie
  9. {
  10. long long val,coefx,coefy;
  11. ecuatie operator+ (const ecuatie &rhs) const
  12. {
  13. return {val+rhs.val,coefx+rhs.coefx,coefy+rhs.coefy};
  14. }
  15. ecuatie operator- (const ecuatie &rhs) const
  16. {
  17. return {val-rhs.val,coefx-rhs.coefx,coefy-rhs.coefy};
  18. }
  19. ecuatie operator* (const ecuatie &rhs) const
  20. {
  21. if(coefx==0&&coefy==0)
  22. return {val*rhs.val,rhs.coefx*val,rhs.coefy*val};
  23. else
  24. return {val*rhs.val,coefx*rhs.val,coefy*rhs.val};
  25. }
  26. };
  27.  
  28. void Ciur()
  29. {
  30. ciur[0]=ciur[1]=1;
  31. for(int i=4;i<=N;i+=2)
  32. ciur[i]=1;
  33. for(int i=3;i*i<=N;++i)
  34. if(ciur[i]==0)
  35. for(int j=i*i;j<=N;j+=2*i)
  36. ciur[j]=1;
  37. for(int i=2;i<=N;++i)
  38. if(!ciur[i])
  39. prim.push_back(i);
  40. }
  41.  
  42. unsigned int poz=0;
  43. ecuatie expresie(string &s);
  44.  
  45. ecuatie numar(string &s)
  46. {
  47. unsigned long long nr=0;
  48. while(poz<s.size()&&isdigit(s[poz]))
  49. {
  50. nr=nr*10+s[poz]-'0';
  51. poz++;
  52. }
  53. return {(long long) nr,0,0};
  54. }
  55.  
  56. ecuatie factor(string &s) ///4x=16
  57. {
  58. ecuatie nr;
  59. nr={0,0,0};
  60. if(s[poz]=='x')
  61. {
  62. nr.coefx=1;
  63. poz++;
  64. }
  65. else
  66. if(s[poz]=='y')
  67. {
  68. nr.coefy=1;
  69. poz++;
  70. }
  71. else
  72. if(s[poz]=='(')
  73. {
  74. poz++;
  75. nr=expresie(s);
  76. poz++;
  77. }
  78. else
  79. {
  80. nr=numar(s);
  81. }
  82. return nr;
  83. }
  84.  
  85. ecuatie termen(string &s)
  86. {
  87. ecuatie nr=factor(s);
  88. while(poz<s.size()&&s[poz]=='*')
  89. {
  90. poz++;
  91. if(s[poz-1]=='*')
  92. nr=nr*factor(s);
  93. }
  94. return nr;
  95. }
  96.  
  97. ecuatie expresie(string &s)
  98. {
  99. ecuatie nr=termen(s);
  100. while(poz<s.size()&&(s[poz]=='+'||s[poz]=='-'))
  101. {
  102. poz++;
  103. if(s[poz-1]=='+')
  104. nr=nr+termen(s);
  105. else
  106. nr=nr-termen(s);
  107. }
  108. return nr;
  109. }
  110.  
  111. int main()
  112. {
  113. string aux,s,s1;
  114. Ciur();
  115. cin>>aux;
  116. for(unsigned int i=0;i<aux.size();++i)
  117. {
  118. if(aux[i]=='x')
  119. X=1;
  120. if(aux[i]=='y')
  121. Y=1;
  122. if(aux[i]=='=')
  123. {
  124. ok=1;
  125. continue;
  126. }
  127. if(i<aux.size()-1&&isdigit(aux[i])&&isalpha(aux[i+1]))
  128. {
  129. if(!ok)
  130. s+=aux[i],s+='*';
  131. if(ok)
  132. s1+=aux[i],s1+='*';
  133. }
  134. else
  135. {
  136. if(!ok)
  137. s+=aux[i];
  138. else
  139. s1+=aux[i];
  140. }
  141. }
  142. ecuatie k=expresie(s);
  143. s=s1;
  144. poz=0;
  145. ecuatie k1=expresie(s);
  146. k1.val-=k.val;
  147. if(Y==0)
  148. {
  149. if(k.coefx==0&&k1.val==0)
  150. {
  151. cout<<"x=2";
  152. return 0;
  153. }
  154. if(k.coefx==0)
  155. {
  156. cout<<"No solution";
  157. return 0;
  158. }
  159. if(k1.val%k.coefx==0&&k1.val/k.coefx>=0&&!ciur[k1.val/k.coefx])
  160. cout<<"x="<<k1.val/k.coefx;
  161. else
  162. cout<<"No solution";
  163. }
  164. else
  165. {
  166. if(k.coefx==0&&k.coefy==0&&k1.val==0)
  167. {
  168. cout<< "x=2,y=2";
  169. return 0;
  170. }
  171. if(k.coefx==0)
  172. {
  173. cout<<"x=2"<<","<<"y="<<k1.val/k.coefy;
  174. return 0;
  175. }
  176. if(k.coefy==0)
  177. {
  178. if(k1.val%k.coefx==0)
  179. cout<<"x="<<k1.val/k.coefx; // aici e schimbarea
  180. else
  181. cout<<"x="<<k1.val/k.coefx<<','<<"y=2";
  182. return 0;
  183. }
  184. for(unsigned int i=0;i<prim.size()&&prim[i]<=k1.val;++i)
  185. {
  186. long long t=1ll*prim[i]*k.coefx;
  187. if((k1.val-t)%k.coefy==0&&(k1.val-t)/k.coefy>=0&&!ciur[(k1.val-t)/k.coefy])
  188. {
  189. cout<<"x="<<prim[i]<<','<<"y="<<(k1.val-t)/k.coefy;
  190. return 0;
  191. }
  192. }
  193. cout<<"No solution";
  194. }
  195. return 0;
  196. }
Add Comment
Please, Sign In to add comment