#include <bits/stdc++.h>
using namespace std;
#define display(arr,s,e) for(i=s; i<=e; i++) cout<<arr[i]<<" ";
#define mset(arr,x) memset(arr,x,sizeof(arr))
# define MOD 4294967296
#define epsilon 0.000000000001
#define I_MAX 9223372036854775807
#define I_MIN -9223372036854775807
#define rep(i,s,e) for(i=s;i<=e;i++)
#define rrep(i,s,e) for(i=s;i>=e;i--)
#define endl "\n"
#define ll long long
#define mid(a,b) ((a)+((b-a)/2))
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
// Useful STL commands:
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define si set<int>
#define vi vector<int>
#define ii pair<int,int>
#define sii set<ii>
#define vii vector<ii>
#define all(c) c.begin(),c.end()
#define tr(c,it) for(auto it=c.begin();it!=c.end();++it)
#define DEBUG
// debugging
#ifdef DEBUG
#define trace1(x) cerr << #x << ": " << x << endl;
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z) cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
#else
#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)
#endif
double tick()
{
static clock_t oldtick; clock_t newtick = clock();
double diff = 1.0*(newtick - oldtick) / CLOCKS_PER_SEC;
oldtick = newtick;
return diff;
}
ll gcd(ll a, ll b)
{
if( (a == 0) || (b == 0) )
return a + b;
return gcd(b, a % b);
}
ll pow_mod(ll a, ll b)
{
ll res = 1;
while(b)
{
if(b & 1)
res = (res * a)%MOD;
a = (a * a)%MOD;
b >>= 1;
}
return res;
}
bool isPrime(ll a)
{
for(int i=3; (i*i)<=a; i+=2)
{
if( (a%i)==0 )
{
return false;
}
}
if( ( a!=2 ) && ( (a%2)==0 ) )
{
return false;
}
return true;
}
string con_ll_to_str( ll a )
{
stringstream mystr;
mystr << a;
return mystr.str();
}
ll con_str_to_ll( string st )
{
ll numb = 0;
int len = st.size(), i, j = 0;
rrep(i, len-1, 0)
{
numb += ( pow_mod(10, j) * ( st[i] - '0' ) );
j++;
}
return numb;
}
// Remember that set and map have the member functions find() and count(), which works in O(log N).
// tick() can be here or there so don't deter and submit it. Check it always on ideone.
// bitset<99999000000000>& c = *(new bitset<99999000000000>()); // For O(1) lookup the max bitset space required.
ll arr[100010];
bool flag;
vector< pair< pair< ll, int > , bool > > vc;
bool comp( pair< pair< ll, int > , bool > pa1, pair< pair< ll, int > , bool > pa2 )
{
if( pa1.f.f==pa2.f.f )
{
return pa1.f.s < pa2.f.s;
}
return pa1.f.f<pa2.f.f;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int i, j, Q, turn = 0;
ll S1, A, B, Si, opop, ans = 0;
cin >> Q >> S1 >> A >> B;
Si = S1;
while( Q-- )
{
opop = Si/2;
if( Si&1 )
{
vc.pb( mp( mp(opop, turn), 1 ) );
}
else
{
vc.pb( mp( mp(opop, turn), 0 ) );
}
Si = ( A*Si ) + B;
if( Si>=MOD )
Si %= MOD;
turn++;
}
sort( all( vc ), comp );
opop = I_MAX;
if( vc[0].s==1 )
{
opop = vc[0].f.f;
ans += opop;
flag = 1;
}
rep(i, 1, turn-1)
{
if( vc[i].f.f==opop && flag && vc[i].s==0 )
{
flag = 0;
ans -= vc[i].f.f;
opop = I_MAX;
}
else if( vc[i].f.f!=opop && vc[i].s==1 )
{
opop = vc[i].f.f;
ans += opop;
flag = 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wKCBwYWlyPCBwYWlyPCBsbCwgaW50ID4gLCBib29sID4gcGExLCBwYWlyPCBwYWlyPCBsbCwgaW50ID4gLCBib29sID4gcGEyICkKewogICAgaWYoIHBhMS5mLmY9PXBhMi5mLmYgKQogICAgewogICAgICAgIHJldHVybiBwYTEuZi5zIDwgcGEyLmYuczsKICAgIH0KCiAgICByZXR1cm4gcGExLmYuZjxwYTIuZi5mOwp9CgppbnQgbWFpbigpCnsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwogICAgY2luLnRpZSgwKTsKCiAgICBpbnQgaSwgaiwgUSwgdHVybiA9IDA7CiAgICBsbCBTMSwgQSwgQiwgU2ksIG9wb3AsIGFucyA9IDA7CgogICAgY2luID4+IFEgPj4gUzEgPj4gQSA+PiBCOwoKICAgIFNpID0gUzE7CiAgICB3aGlsZSggUS0tICkKICAgIHsKICAgICAgICBvcG9wID0gU2kvMjsKICAgICAgICBpZiggU2kmMSApCiAgICAgICAgewogICAgICAgICAgIHZjLnBiKCBtcCggbXAob3BvcCwgdHVybiksIDEgKSApOwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgewogICAgICAgICAgIHZjLnBiKCBtcCggbXAob3BvcCwgdHVybiksIDAgKSApOwogICAgICAgIH0KCiAgICAgICAgU2kgPSAoIEEqU2kgKSArIEI7CiAgICAgICAgaWYoIFNpPj1NT0QgKQogICAgICAgICAgICBTaSAlPSBNT0Q7CgogICAgICAgIHR1cm4rKzsKICAgIH0KICAgIHNvcnQoIGFsbCggdmMgKSwgY29tcCApOwoKICAgIG9wb3AgPSBJX01BWDsKICAgIGlmKCB2Y1swXS5zPT0xICkKICAgIHsKICAgICAgICBvcG9wID0gdmNbMF0uZi5mOwogICAgICAgIGFucyArPSBvcG9wOwogICAgICAgIGZsYWcgPSAxOwogICAgfQogICAgcmVwKGksIDEsIHR1cm4tMSkKICAgIHsKICAgICAgICBpZiggdmNbaV0uZi5mPT1vcG9wICYmIGZsYWcgJiYgdmNbaV0ucz09MCApCiAgICAgICAgewogICAgICAgICAgICBmbGFnID0gMDsKICAgICAgICAgICAgYW5zIC09IHZjW2ldLmYuZjsKICAgICAgICAgICAgb3BvcCA9IElfTUFYOwogICAgICAgIH0KICAgICAgICBlbHNlIGlmKCB2Y1tpXS5mLmYhPW9wb3AgJiYgdmNbaV0ucz09MSApCiAgICAgICAgewogICAgICAgICAgICBvcG9wID0gdmNbaV0uZi5mOwogICAgICAgICAgICBhbnMgKz0gb3BvcDsKICAgICAgICAgICAgZmxhZyA9IDE7CiAgICAgICAgfQogICAgICAgIGVsc2UKICAgICAgICB7CiAgICAgICAgICAgIG9wb3AgPSBJX01BWDsKICAgICAgICB9CiAgICB9CgogICAgY291dCA8PCBhbnM7CgoKICAgIGNlcnI8PHRpY2soKTsKICAgIHJldHVybiAwOwp9Cg==