Problem: Example

Input

1 1 1 1 1


Output

0.000000000000

Solution (in CPP):

Solution:
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
typedef long long ll;

#define mod(a,b) ((((a)%(b))+(b))%(b))

ll thesum(ll n,ll p,ll qq,ll rr){ //sum{i=1..n}[(qq*i+rr)/p]
	ll q=qq%p;if(q<0)q+=p;
	ll r=rr%p;if(r<0)r+=p;
	ll last=(n*q+r)/p;
	if(q==0)return (qq-q)/p*n*(n+1)/2+(rr-r)/p*n;
	else return (qq-q)/p*n*(n+1)/2+(rr-r)/p*n+(n+1)*last-thesum(last,q,p,q-1-r);
}

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}

ll maxrem(ll n,ll p,ll q){ //i in {0,...,n-1} such that i*q%p attains max (q<p, n<=(period of seq.))
	if(q==0)return 0;
	ll lp=1,lq=0;
	ll up=0,uq=1;
	for(;;){
		ll maxk;
		maxk=(q*up-p*uq)/(p*lq-q*lp);
		if(up+maxk*lp<n-1){up+=maxk*lp;uq+=maxk*lq;}
		else{
			ll k=(n-1-up)/lp;
			up+=k*lp;
			uq+=k*lq;
			return up;
		}
		maxk=(q*lp-p*lq-1)/(p*uq-q*up);
		lp+=maxk*up;lq+=maxk*uq;
	}
}

double solve(ll w,ll d,ll n,ll m,ll t){
	ll v=w+d;
	ll l=w+1;
	ll g=gcd(v,t);
	double ans;
	if(l>=t)ans=0;
	else{
		if(t/gcd(t,m)<n){ //cutting-point ga kaburu
			ll cnt=thesum(t/gcd(t,m),g,gcd(t,m),0)-thesum(t/gcd(t,m),g,gcd(t,m),-min(gcd(t,m),l));
			ans=(double)(t/g-cnt)/(t/g);
		}else{ //cutting-point ga kaburanai
			ll i=maxrem(n,t,mod(-m,t));
			ll j=maxrem(n,t,mod(m,t));
			ll d1=t-mod(-m*i,t);
			ll d2=t-mod(m*j,t);
			ll cnt=0;
			cnt+=thesum(n-i,g,-m,m)-thesum(n-i,g,-m,m-min(d1,l));
			cnt+=thesum(i+j-n,g,-m,-m*(n-i-1))-thesum(i+j-n,g,-m,-m*(n-i-1)-min(d1+d2,l));
			cnt+=thesum(n-j,g,-m,-m*(j-1))-thesum(n-j,g,-m,-m*(j-1)-min(d2,l));
			ans=(double)(t/g-cnt)/(t/g);
		}
	}
	return ans;
}

int main(){
	ll w,d,n,m,t;
	scanf("%lld%lld%lld%lld%lld",&w,&d,&n,&m,&t);
	printf("%.15f\n",solve(w,d,n,m,t));
}