Problem:
B: P進XOR
問題
P
進数(
P
は素数)でのXOR
X\ ⊕\ Y = Z
を次のように定義します。
X, Y, Z
を
P
進法で表記した各桁を
X_i, Y_i, Z_i
とすると、
Z_i
は
X_i + Y_i
を
P
で割った余り
N
までの非負整数の
P
進数での累積XOR
0⊕1⊕...⊕N
を求め、
10
進数で出力してください。
入力形式
N
P
制約
1\leq N\leq 10^{12}
2\leq P\leq 10^5
P
は素数
入力はすべて整数
出力形式
N
までの非負整数の
P
進数での累積XORを出力してください。
入力例1
10 2
出力例1
11
0\ ⊕\ 1\ ⊕\ 10\ ⊕\ 11\ ⊕\ 100\ ⊕\ ...⊕\ 1010 = 1011
となり、これは
10
進数では
11
です。
入力例2
10 3
出力例2
19
入力例3
1000000000000 99991
出力例3
101710000987989
オーバーフローに注意してください。

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, a, b) for (ll i = a; i < b; i++)
const int INF = (1 << 29);

int main(){
    ll N;
    cin >> N;
    ll P;
    cin >> P;
    N++;
    ll M = (N / (2 * P)) * P * 2; 
    const int D = 60;
    vector<ll> ans(D);
    rep(i, M, N){
        ll tmp = i;
        rep(rp, 0, D){
            ans[rp] = (ans[rp] + tmp % P) % P;
            tmp /= P;
        }
    }
    ll val = 0;
    for (int d = D - 1; d >= 0; d--){
        val *= P;
        val += ans[d];
    }
    cout << val << "\n";
}
accuracy: 1
time_ms: 90
memory_kb: 3432
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> iint;
typedef pair<ll,ll> llll;
#define ALL(x) (x).begin(),(x).end()
const ll zero = 0;
const ll one = 1;
const ll INF = 9223372036854775807; //10^18
const int inINF = 2147483647; //10^9
const ll MOD = 1000000007; //10^9+7
const ll MOD2 = 998244353;
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}

int main(){
    ll N, P;
    cin >> N >> P;
    ll M = (N / (2 * P)) * 2 * P;
    vector<ll> ans(45, 0);

    for (ll i = M; i < N+1; i++) {
        vector<ll> tmp(45, 0);
        ll t = i;
        for (ll j = 0; j < 45; j++) {
            tmp[j] = t % P;
            t = t / P;
        }
        for (ll j = 0; j < 45; j++) {
            ans[j] = (tmp[j] + ans[j]) % P;
        }
    }

    ll p = 1;
    ll answer = 0;
    for (ll i = 0; i < 45; i++) {
        answer += p * ans[i];
        p = p * P;
    }
    printf("%lld\n", answer);

}
accuracy: 1
time_ms: 80
memory_kb: 3564
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define ll long long
#define pp pair<ll,ll>
#define ld long double
#define all(a) (a).begin(),(a).end()
#define mk make_pair
int inf=1000001000;
ll INF=1e18;
ll mod=1000000007;
ll MOD=INF;

int main() {
    ll n,p;
    cin >> n >> p;
    ll c=0;
    rep(i,p) (c+=i)%=p;
    vector<vector<int>> a(0,vector<int>(50));
    for (ll i=n-(n%p);i<=n;i++){
        vector<int> e(50);
        ll y=i;
        rep(j,50){
            e[j]=y%p;
            y/=p;
        }
        a.push_back(e);
    }
    ll ans=0;
    rep(i,50){
        ll w=0;
        rep(j,a.size()){
            (w+=a[j][i])%=p;
        }
        if (i==0) (w+=c*(n/p))%=p;
        ll t=1;
        rep(j,i) t*=p;
        ans+=t*w;
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 50
memory_kb: 14180
final_rank: 8

code_snippet: #include "bits/stdc++.h"
#define YES "YES"
#define NO "NO"
#define Yes "Yes"
#define No "No"
#define DEFAULT LL ans=solve();\
 	if(ans==NONE){\
 	}else{\
 		cout << ans << endl;\
 	}
#define YESNO three(solve(),(OUT(YES),1),(OUT(NO),0))
#define YesNo three(solve(),OUT(Yes),OUT(No))
#define ECHO OUT(solve())
#define three(A,B,C) ((A)?(B):(C))
#define FOR(i,a,b)  for(LL i=(a);i< (LL)(b);i++)
#define EFOR(i,a,b) for(LL i=(a);i<=(LL)(b);i++)
#define RFOR(i,a,b) for(LL i=(b-1);i>=(LL)(a);i--)
#define REP(i,b) FOR(i,zero,b)
#define rep REP
#define EREP(i,b) EFOR(i,zero,b)
#define RREP(i,b) RFOR(i,zero,b)
#define ALL(c) c.begin(),c.end()
#define UNIQUE(c) sort(ALL(c));c.erase(unique(ALL(c)),c.end())
#define MAX(c) (*max_element(ALL(c)))
#define MIN(c) (*min_element(ALL(c)))
#define MP make_pair
#define FI first
#define SE second
#define SI(x) (LL(x.size()))
#define PB push_back
#define DEBUG(a) OUT(a)
#define DEBUG2(a,b) OUT2(a,b)
#define cat cout << __LINE__ << endl
#define OUT(a) cout << (a) << endl
#define OUT2(a,b) cout << (a) <<" "<<(b) << endl
#define zero 0LL
#define all ALL
#define pb emplace_back
#define eb pb
#define int long long
using namespace std;
template<typename T> inline void maximize(T &a, T b) { a = max(a, b); }
template<typename T> inline void minimize(T &a, T b) { a = min(a, b); }
template<typename T> inline bool middle(T a, T b, T c) { return b <= a && a <= c; }
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; }
typedef int LL;
typedef double ld;
typedef int ut;
typedef vector<ut> VI;
typedef vector<VI> VII;
typedef pair<ut, ut> pr;
typedef pair<ut, pr> ppr;
typedef vector<pr> Vpr;
typedef vector<ppr> Vppr;
typedef tuple<int, int, int, int> tapu;
typedef vector<tapu> Vtapu;
typedef priority_queue<pr, Vpr > PQ;
inline void outputVI(VI x) { REP(i, SI(x)) { cout << three(i, " ", "") << x[i]; }OUT(""); }
const int SIZE1 = 5e5 + 1000;
const int SIZE2 = 5010;
const int SIZE3 = 430;
const int SIZE = SIZE1;
const int MAPSIZE = 40;
const LL p998 = 998244353;
const LL p107 = 1000000007;
const LL p = p107;
const LL INF = 1LL << 60;
const long double EPS = 1e-7;
const LL NONE=-2;
#define endl "\n"
ut N, M,K,  Q, D, H, W;
VI edges[SIZE];
LL vals[SIZE], answer = zero;
LL A[SIZE],B[SIZE],C[SIZE];
LL solve(){
	LL N,P;
	cin >> N >> P;
	LL c=N%(2*P);
	LL ans=0;
	EFOR(i,N-c,N){
		LL x=i;
		REP(j,100){
			A[j]+=x%P;
			A[j]%=P;
			x/=P;
		}
	}
	RREP(i,100){
		ans*=P;
		ans+=A[i];
	}
	return ans;	
/*	if(N%2==1)	return (N/2)%2;
	else return N^(((N-1)/2)%2);
	while(NX){
		LL c=NX%P;
		NX/=P;
		times*=P;
		LL x=c*(c-1)/2%P*times%P+(P*(P-1)/2)%P*NX%P*times%P;
		ans+=x*times;
	}
	return ans;*/
	return NONE;
}
 signed main() {
 
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(10);
	LL T=1;

	REP(i,T){
		DEFAULT;

	}
	
//	YesNo;
//	YESNO;
//	three(solve(),OUT("First"),OUT("Second"));
	//cin >> N;
	return 0;
 
}
accuracy: 1
time_ms: 170
memory_kb: 14976
final_rank: 9

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

typedef long long ll;

int main() {
    ll N, P, S;
    cin >> N >> P;
    if (P == 2) S = N / 4 * 4;
    else S = N / P * P;
    //cout << "S=" << S << "\n";
    vector<int> A(40, 0);
    for (ll i = S; i <= N; i++) {
        ll x = i;
        rep(j, 40) {
            A[j] = (A[j] + x % P) % P;
            x /= P;
        }
    }
    ll ans = 0;
    rep(i, 40) {
        ans *= P;
        ans += A[39 - i];
        //cout << "A[" << i << "]=" << A[i] << "\n";
    }
    cout << ans << "\n";
}
accuracy: 1
time_ms: 20
memory_kb: 3124
final_rank: 2

code_snippet: /**
 *    author:  otera    
**/
#include<iostream>
#include<string> 
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm> 
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
using namespace std;

#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

void solve() {
	int n, p; cin >> n >> p;
    int pp = p;
    if(p == 2) {
        pp = 4;
    }
    int ans = 0;
    vector<int> a(60, 0);
    for(int i = (n / pp) * pp; i <= n; ++ i) {
        int pw = 1;
        int now = i;
        rep(j, 60) {
            if(pw * p > n) break;
            int k = now % p;
            a[j] = (a[j] + k) % p;
            now /= p;
        }
    }
    int pw = 1;
    rep(i, 60) {
        ans += pw * a[i];
        pw *= p;
    }
    cout << ans << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}
accuracy: 0.15789473684210525
time_ms: 10
memory_kb: 3168
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
const long long INF = 10000000000000;
vector<int> change(long long a, int p){
  vector<int> ans;
  for (int i = 0; i < 50; i++){
    ans.push_back(a % p);
    a /= p;
  }
  return ans;
}
long long to_dec(vector<int> a, int p){
  long long b = 1;
  long long ans = 0;
  for (int i = 0; i < 50; i++){
    ans += a[i] * b;
    b *= p;
    if (b > INF){
      break;
    }
  }
  return ans;
}
long long p_xor(long long a, long long b, int p){
  vector<int> A = change(a, p);
  vector<int> B = change(b, p);
  vector<int> C(45);
  for (int i = 0; i < 45; i++){
    C[i] = (A[i] + B[i]) % p;
  }
  return to_dec(C, p);
}
int main(){
  long long N;
  long long P;
  cin >> N >> P;
  long long M = N / (P * 2) * (P * 2);
  long long ans = 0;
  for (long long i = M; i <= N; i++){
    ans = p_xor(ans, i, P);
  }
  cout << ans << endl;
}
accuracy: 1
time_ms: 260
memory_kb: 3172
final_rank: 7

code_snippet: /**
 *    author:  otera    
**/
#include<iostream>
#include<string> 
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm> 
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
using namespace std;

#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

void solve() {
	int n, p; cin >> n >> p;
    int pp = p;
    if(p == 2) {
        pp = 4;
    }
    int ans = 0;
    vector<int> a(60, 0);
    for(int i = (n / pp) * pp; i <= n; ++ i) {
        int pw = 1;
        int now = i;
        rep(j, 60) {
            if(pw * p > n) break;
            int k = now % (pw * p);
            a[j] = (a[j] + k) % p;
            now /= (pw * p);
        }
    }
    int pw = 1;
    rep(i, 60) {
        ans += pw * a[i];
        pw *= p;
    }
    cout << ans << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}
accuracy: 0.15789473684210525
time_ms: 10
memory_kb: 3160
final_rank: 10

code_snippet: #include <iostream>
using namespace std;
typedef long long ll;

int main()
{
    ll n, p;
    cin >> n >> p;
    if(p == 2){
        ll ans = 0;
        for(ll i = n / 4 * 4; i <= n; i++) ans ^= i;
        cout << ans << endl;
        return 0;
    }
    ll d[50]{0};
    for(ll i = n / p * p; i <= n; i++){
        ll l = i;
        for(int j = 0; j < 50; j++){
            d[j] += l % p;
            l /= p;
        }
    }
    ll q = 1;
    ll ans = 0;
    for(int j = 0; j < 50; j++){
        ans += d[j] % p * q;
        q *= p;
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3428
final_rank: 3

code_snippet: #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iomanip>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cstdint>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(),(x).end()

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}
 
template<class T> inline bool chmax(T &a, T b){
    if(a<b){
        a = b;
        return true;
    }
    return false;
}

template<class T> inline bool chmin(T &a, T b){
    if(a>b){
        a = b;
        return true;
    }
    return false;
}

int main(){
    ll n,p;
    cin >> n >> p;
    if(p==2){
        ll K = n/4 * 4;
        vector<ll> a(60);
        for(ll i=K;i<=n;i++){
            rep(j,60){
                if((i>>j)&1){
                    a[j]++;
                    a[j]%=2;
                }
            }
        }
        ll res = 0;
        rep(j,60){
            res += (1LL<<j)*a[j];
        }
        cout << res << endl;
    }else{
        ll K = n/p * p;
        vector<ll> a(60);
        for(ll i=K;i<=n;i++){
            ll X = i;
            rep(j,60){
                a[j] += X%p;
                X/=p;
                a[j] %= p;
            }
        }
        int mxid = 0;
        rep(j,60){
            if(a[j]>0)mxid = j;
        }
        ll res = 0;
        ll tmp = 1;
        rep(j,mxid+1){
            res += tmp*a[j];
            tmp *=p;
        }
        cout << res << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3432
final_rank: 4

code_snippet: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"


using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> N >> K;
	M = N / 2 / K * 2 * K;
	long long int ans = 0;
	long long int cnt = K;
	while (1) {
		long long int add = 0;
		for (long long int i = M; i <= N; i++) {
			add += i / (cnt / K) % K;
			add %= K;
		}
		add *= cnt/K;
		ans += add;
		if (N < cnt) {
			break;
		}
		cnt *= K;
	}
	cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3228
final_rank: 1