Problem:
E: Expensive Function
問題文
数列$a$と非負整数上の関数$f(x)$が以下のように定義されています。
$a_1 = 0$
$a_i = (a_{i-1} \times p + q) \bmod M (i \geq2)$
$f(x) = x \mbox{ XOR } a_1 \mbox{ XOR } a_2 \mbox{ XOR } ... \mbox{ XOR } a_{10^8}$
松崎くんが計算すると、$f(s) = t$ でした。
非負整数 $y$ が与えられるので、$f(y)$ を求めてください。
$n$ 個の非負整数 $x_1, x_2, \ldots, x_n$ の排他的論理和 $x_1 \mbox{ XOR } x_2 \mbox{ XOR } \ldots \mbox{ XOR } x_n$ は以下のように定義されます:
$x_1 \mbox{ XOR } x_2 \mbox{ XOR } \ldots \mbox{ XOR } x_n$ を二進表記した際の $2^k ( k \geq 0 )$ の位の数は $x_1, x_2, \ldots, x_n$ のうち、二進表記した際の $2^k ( k \geq 0 )$ の位の数が $1$ となるものの個数が奇数ならば $1$ 、そうでなければ $0$ となる。
制約
$0 \leq p, q, s, t, y \leq 10^9$
$1 \leq M \leq 10^9$
入力は全て整数
入力
入力は以下の形式で標準入力から与えられます。
$s$  $t$
$p$ $q$ $M$
$y$
出力
$f(y)$ を出力してください。
入出力例
入力例1
0 15656
3 7 13333
0
出力例1
15656
入力例2
0 0
0 0 3
0
出力例2
0
入力例3
1000000000 1000000000
1000000000 1000000000 1000000000
1000000000
出力例3
1000000000

Submitted solutions:

code_snippet: #include <bits/stdc++.h>

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);

    int s, t;
    std::cin >> s >> t;
    long long p, q, M;
    std::cin >> p >> q >> M;
    int all{}, a{};
    for (int i{2} ; i <= 100000000 ; i++) {
        a = ((long long)a * p + q) % M;
        all ^= a;
    }
    int y;
    std::cin >> y;
    std::cout << (all ^ y) << std::endl;
}
accuracy: 1
time_ms: 980
memory_kb: 3452
final_rank: 15

code_snippet: #include<bits/stdc++.h>
//#include<atcoder/all>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
//using namespace atcoder;
typedef long long ll;
const ll INF = 1e18;
//typedef modint998244353 mint;
//cout << fixed << setprecision(12);

int main(){
	int s,t;
	ll p,q,m;
	cin >> s >> t;
	cin >> p >> q >> m;
	ll a=0;
	ll y;
	cin >> y;
	for (int i=0; i<99999999; i++){
		a = (a*p%m+q)%m;
		y ^= a;
	}
	cout << y << endl;
}
accuracy: 1
time_ms: 2250
memory_kb: 3088
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;

ll s, t, p, q, M, y;

int main()
{
    scanf("%lld%lld", &s, &t);
    scanf("%lld%lld%lld", &p, &q, &M);
    scanf("%lld", &y);
    ll Res=0, a=0;
    Res^=y;
    for (int i=2; i<=1e8; i++)
    {
        a=(a*p+q)%M;
        Res^=a;
    }
    printf("%lld\n", Res);
}
accuracy: 1
time_ms: 1200
memory_kb: 3212
final_rank: 11

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


ll s,t;
ll p,q,M,y;


int main(){

	scanf("%lld %lld",&s,&t);

	scanf("%lld %lld %lld %lld",&p,&q,&M,&y);

	ll ans = y;
	ll a = 0;

	ans ^= a;

	for(int i = 2; i <= 100000000; i++){

		a = (a*p+q)%M;
		ans ^= a;
	}

	printf("%lld\n",ans);


	return 0;
}
accuracy: 1
time_ms: 1210
memory_kb: 3228
final_rank: 12

code_snippet: #include <bits/stdc++.h>

#define ll long long

using namespace std;

int main()
{
	ll s, t; cin>>s>>t;
	ll p, q, M; cin>>p>>q>>M;
	ll y; cin>>y;

	const ll MAX = 1e8;

	ll ans = y;
	ll a = 0;
	for(int i=1;i<MAX;i++){
		a = (a*p+q)%M;
		ans = ans xor a;
	}
	cout << ans << "\n";
	return 0;
}
accuracy: 1
time_ms: 1200
memory_kb: 3084
final_rank: 6

code_snippet: #pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
 
#pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector")
#pragma GCC diagnostic error "-fwhole-program"
#pragma GCC diagnostic error "-fcse-skip-blocks"
#pragma GCC diagnostic error "-funsafe-loop-optimizations"

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int packSize = 8;
const int packSizeH = 4;

int main(){
	ll s,t;
	ll p,q,M;
	ll y;
	cin>>s>>t;
	cin>>p>>q>>M;
	cin>>y;
	p %= M;
	q %= M;

	// (((x*p+q)*p+q)*p+q)*p+q
	// p^4*x + p^3*q + p^2*q + p*q + q
	// p^4*x + (((p+1)*p+1)*p+1)*q

	ll P = p * p % M * p % M * p % M * p % M * p % M * p % M * p % M;
	ll Q = (((((((p + 1) * p % M + 1) * p % M + 1)* p % M + 1)* p % M + 1)* p % M + 1)* p % M + 1) * q % M;

	ll r[packSize] = {y};
	ll a[packSize] = {0};

	for(int i = 1; i < packSize; ++i){
		a[i] = (a[i-1]*p + q)%M;
	}
	
	const int T = 1e8;
	for(int i = 0; i < T; i+=packSize){
		for(int j = 0; j < packSizeH; ++j){
			r[j] ^= a[j];
			a[j] = (a[j]*P+Q)%M;
 		}
		for (int j = packSizeH; j < packSize; ++j) {
			r[j] ^= a[j];
			a[j] = (a[j]*P+Q)%M;
 		}
	}
	ll res = r[0];
	for(int i = 1; i < packSize; ++i){
		res ^= r[i];
	}
	cout << res << endl;
	return 0;
}
accuracy: 1
time_ms: 650
memory_kb: 3084
final_rank: 3

code_snippet: #pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
 
#pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector")
#pragma GCC diagnostic error "-fwhole-program"
#pragma GCC diagnostic error "-fcse-skip-blocks"
#pragma GCC diagnostic error "-funsafe-loop-optimizations"

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int packSize = 8;

int main(){
	ll s,t;
	ll p,q,M;
	ll y;
	cin>>s>>t;
	cin>>p>>q>>M;
	cin>>y;
	p %= M;
	q %= M;

	// (((x*p+q)*p+q)*p+q)*p+q
	// p^4*x + p^3*q + p^2*q + p*q + q
	// p^4*x + (((p+1)*p+1)*p+1)*q

	ll P = p * p % M * p % M * p % M * p % M * p % M * p % M * p % M;
	ll Q = (((((((p + 1) * p % M + 1) * p % M + 1)* p % M + 1)* p % M + 1)* p % M + 1)* p % M + 1) * q % M;

	ll r[packSize] = {y};
	ll a[packSize] = {0};

	for(int i = 1; i < packSize; ++i){
		a[i] = (a[i-1]*p + q)%M;
	}
	
	const int T = 1e8;
	for(int i = 0; i < T; i+=packSize){
		for(int j = 0; j < packSize; ++j){
			r[j] ^= a[j];
			a[j] = (a[j]*P+Q)%M;
 		}
	}
	ll res = r[0];
	for(int i = 1; i < packSize; ++i){
		res ^= r[i];
	}
	cout << res << endl;
	return 0;
}
accuracy: 1
time_ms: 650
memory_kb: 3084
final_rank: 3

code_snippet: #pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
 
#pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector")
#pragma GCC diagnostic error "-fwhole-program"
#pragma GCC diagnostic error "-fcse-skip-blocks"
#pragma GCC diagnostic error "-funsafe-loop-optimizations"

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int packSize = 4;

int main(){
	ll s,t;
	ll p,q,M;
	ll y;
	cin>>s>>t;
	cin>>p>>q>>M;
	cin>>y;
	p %= M;
	q %= M;

	// (((x*p+q)*p+q)*p+q)*p+q
	// p^4*x + p^3*q + p^2*q + p*q + q
	// p^4*x + (((p+1)*p+1)*p+1)*q

	ll P = p * p % M * p % M * p % M;
	ll Q = (((p + 1) * p % M + 1) * p % M + 1) * q % M;

	ll r[packSize] = {y};
	ll a[packSize] = {0};

	for(int i = 1; i < packSize; ++i){
		a[i] = (a[i-1]*p + q)%M;
	}
	
	const int T = 1e8;
	for(int i = 0; i < T; i+=packSize){
		for(int j = 0; j < packSize; ++j){
			r[j] ^= a[j];
			a[j] = (a[j]*P+Q)%M;
 		}
	}
	ll res = r[0];
	for(int i = 1; i < packSize; ++i){
		res ^= r[i];
	}
	cout << res << endl;
	return 0;
}
accuracy: 1
time_ms: 640
memory_kb: 3084
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int packSize = 256/64;

int main(){
	ll s,t;
	ll p,q,M;
	ll y;
	cin>>s>>t;
	cin>>p>>q>>M;
	cin>>y;
	p %= M;
	q %= M;

	// (((x*p+q)*p+q)*p+q)*p+q
	// p^4*x + p^3*q + p^2*q + p*q + q
	// p^4*x + (((p+1)*p+1)*p+1)*q
	
	ll P = (((p*p)%M*p)%M)*p%M;
	ll Q = (((p+1)*p%M +1)*p%M+1)*q %M;

	ll r[packSize] = {y};
	ll a[packSize] = {0};

	for(int i = 1; i < packSize; ++i){
		a[i] = (a[i-1]*p + q)%M;
	}
	
	const int T = 1e8;
	for(int i = 0; i < T; i+=packSize){
		for(int j = 0; j < packSize; ++j){
			r[j] ^= a[j];
			a[j] = (a[j]*P+Q)%M;
 		}
	}
	ll res = r[0];
	for(int i = 1; i < packSize; ++i){
		res ^= r[i];
	}
	cout << res << endl;
	return 0;
}
accuracy: 1
time_ms: 690
memory_kb: 3084
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define int long long

signed main(){

	int s,t;
	int a,b,M;
	int x;

	cin>>s>>t;
	cin>>a>>b>>M;
	cin>>x;

	a%=M;
	b%=M;

	int f=0;


	// 1e8回
	int T=100000000;
	for(int p=0,i=0;i<T;i++){
		f^=p;
		p = ((p*a)%M + b)%M;

	}


	cout<< (f^x) <<endl;

	return 0;
}
accuracy: 1
time_ms: 2220
memory_kb: 3084
final_rank: 13

code_snippet: #pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
 
#pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector")
#pragma GCC diagnostic error "-fwhole-program"
#pragma GCC diagnostic error "-fcse-skip-blocks"
#pragma GCC diagnostic error "-funsafe-loop-optimizations"

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int packSize = 256/64;

int main(){
	ll s,t;
	ll p,q,M;
	ll y;
	cin>>s>>t;
	cin>>p>>q>>M;
	cin>>y;
	p %= M;
	q %= M;

	// (((x*p+q)*p+q)*p+q)*p+q
	// p^4*x + p^3*q + p^2*q + p*q + q
	// p^4*x + (((p+1)*p+1)*p+1)*q
	
	ll P = (((p*p)%M*p)%M)*p%M;
	ll Q = (((p+1)*p%M +1)*p%M+1)*q %M;

	ll r[packSize] = {y};
	ll a[packSize] = {0};

	for(int i = 1; i < packSize; ++i){
		a[i] = (a[i-1]*p + q)%M;
	}
	
	const int T = 1e8;
	for(int i = 0; i < T; i+=packSize){
		for(int j = 0; j < packSize; ++j){
			r[j] ^= a[j];
			a[j] = (a[j]*P+Q)%M;
 		}
	}
	ll res = r[0];
	for(int i = 1; i < packSize; ++i){
		res ^= r[i];
	}
	cout << res << endl;
	return 0;
}
accuracy: 1
time_ms: 640
memory_kb: 3084
final_rank: 1

code_snippet: #include <iostream>
#include <fstream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define rep2(i, s, n) for(int i=s; i<(n); ++i)
#define sz(x) int(x.size())

typedef long long ll;

using namespace std;

static const int MAX = 100001;

/******* c++14 don't include *******/

template<typename T>
T gcd(T a, T b){if( b == 0)return a; return gcd(b, a%b); }
template<typename T>
T lcm(T a, T b){return a*b/gcd(a,b); }

/*******                      *******/

int main(int argc, const char * argv[]) {
    // input from txt
    /////////
    //write//
    /////////
    
    ll s, t;
    cin >> s >> t;
    ll p, q, m;
    cin >> p >> q >> m;
    ll y;
    cin >> y;
    
    ll a = 0;
    rep(i, 100000000){
        if(i)a = (a * p + q) % m;
        y ^= a;
    }
    
    cout << y << endl;
    
    
    return 0;
}
accuracy: 1
time_ms: 1210
memory_kb: 3120
final_rank: 9

code_snippet: #include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
typedef pair<int,int>P;

int main(){
	int s,t,p,q,M,y;cin>>s>>t>>p>>q>>M>>y;
	ll a=q;
	int L;
	for(int i=1;;i++){
		if(a==0){
			L=i;
			break;
		}
		a=(a*p+q)%M;
	}
	ll C=100000000%L;
	a=0;
	ll ans=0;
	rep(i,C){
		ans^=a;
		a=(a*p+q)%M;
	}
	cout<<(ans^y)<<endl;
}
accuracy: 0.07692307692307693
time_ms: 460
memory_kb: 3044
final_rank: 16

code_snippet: #include <bits/stdc++.h>

#define ll long long

using namespace std;


int main(){
	ll s, t; cin>>s>>t;
	ll p, q, M; cin>>p>>q>>M;
	ll y; cin>>y;
	ll a = 0;
	ll ans = y;
	for(int i=1;i<1e8;i++) {
		a = (a*p+q) % M;
		ans = ans ^ a;

	}
	cout << ans << "\n";
	return 0;
}
accuracy: 1
time_ms: 1200
memory_kb: 3092
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define int long long

signed main() {
  int s, t, p, q, M, y;
  cin >> s >> t >> p >> q >> M >> y;

  int sum = y;
  int a = 0;
  int cnt = 1e8;  
  for ( int i = 0; i < cnt; i++ ) {
    sum ^= a;
    a = (a*p + q)%M;
  }

  cout << sum << endl;
  
  return 0;
}
accuracy: 1
time_ms: 1200
memory_kb: 3088
final_rank: 7

code_snippet: #include<bits/stdc++.h>
#define X first
#define Y second
#define pb emplace_back
#define FOR(i,a,b) for(int (i)=(a);i<(b);++(i))
#define EFOR(i,a,b) for(int (i)=(a);i<=(b);++(i))
#define rep(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define REP rep
#define rrep(X,Y) for (int (X) = (Y)-1;(X) >=0;--(X))
#define all(X) (X).begin(),(X).end()
#define eb emplace_back

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef ll LL;
typedef pii PII;
typedef pll PLL;
const ll MOD=1e9+7;

#define rall(X) (X).rbegin(),(X).rend()
#define UNIQUE(X) (X).erase(unique(all(X)),(X).end())
#define reps(X,S,Y) for (int (X) = S;(X) < (Y);++(X))
#define rreps(X,S,Y) for (int (X) = (Y)-1;(X) >= (S);--(X))
template<class T> inline bool MX(T &l,const T &r){return l<r?l=r,1:0;}
template<class T> inline bool MN(T &l,const T &r){return l>r?l=r,1:0;}

LL p, q, s, t, y;
LL M;

signed main(){
  ios_base::sync_with_stdio(false);
  cout<<fixed<<setprecision(0);

  cin >> s >> t;
  cin >> p >> q >> M;
  cin >> y;

  LL a = 0;
  LL xs = 0;
  reps(i, 2, 100000001) {
    a = (a*p + q) % M;
    xs ^= a;
  }
  cout << (y ^ xs) << endl; 
}
accuracy: 1
time_ms: 1200
memory_kb: 3184
final_rank: 10