Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <iostream>
- #include <iomanip>
- using namespace std;
- double lambertW(double x, double prec=1e-8){
- double w = 0;
- for(int i=0;i<100;i++){
- double wTimesExpW = w*exp(w);
- double wPlusOneTimesExpW = (w+1)*exp(w);
- w -= (wTimesExpW-x)/(wPlusOneTimesExpW-(w+2)*(wTimesExpW-x)/(2*w+2));
- //cout <<w<<endl;
- if (prec > abs((x-wTimesExpW)/wPlusOneTimesExpW)){
- return w;
- }
- /* if (prec <= abs((x-wTimesExpW)/wPlusOneTimesExpW)){
- cout <<"W(x) ne shoditsa x=" <<x<<endl;}*/
- }
- return NAN;
- }
- double lambertW1(double x, double prec=1e-12){
- double w=1.0;
- for(int i=0;i<100;i++){
- w=-log(-x/w);
- //cout <<w<<endl;
- }
- return w;
- }
- double result(double x){
- return round(x*100000000)/100000000;
- }
- int main()
- {
- double a,b;
- cin>>a>>b;
- if(a==0){
- if(b<=0)
- cout<<"no solution exists";
- else
- cout<<fixed<<setprecision(9)<<log(b);
- return 0;
- }
- if(a==1&&b==1){
- cout<<0;
- return 0;
- }
- double x1=(-b/a)-lambertW(-1/(a*pow(M_E,b/a)));
- double x2=(-b/a)+lambertW1(-1/(a*pow(M_E,b/a)));
- x1=result(x1);
- x2=result(x2);
- if(x1==0)x1=abs(x1);
- if(x2==0)x2=abs(x2);
- if(isnan(x1)) {
- if(isnan(x2)) {
- cout<<"no solution exists";
- return 0;
- }else{
- cout<<fixed<<setprecision(8)<<x2;
- return 0;
- }
- }else{
- if(isnan(x2)) {
- cout<<fixed<<setprecision(8)<<x1;
- return 0;
- }
- }
- if(x1<x2)
- cout<<fixed<<setprecision(8)<<x1<<" "<<fixed<<setprecision(8)<<x2;
- else
- cout<<fixed<<setprecision(8)<<x2<<" "<<fixed<<setprecision(8)<<x1;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement