424C - Magic Formulas - CodeForces Solution


math *1600

Please click on ads to support us..

Python Code:

n = int(input())
arr = list(map(int,input().split()))

xor = [0] * (n+1)

x = 0
for i in range(1,n+1):
	x ^= i
	xor[i] = x

ans = 0

for i in range(1,n+1):
	q = n // i
	r = n % i
	ans ^= arr[i-1]
	if q % 2:
		ans ^= xor[i-1] 
		if r:
			ans ^= xor[r]
	else:
		if r:
			ans ^= xor[r]

print(ans)

C++ Code:

//
// Created by Xiuyuan Cao on 2023/2/22.
//

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <limits>
using namespace std;

typedef long long ll;


void solve(){
    int n;
    cin>>n;
    int p[n];
    for(int i=0;i<n;i++){
        cin>>p[i];
    }
    int res=0;
    for(int i:p){
        res^=i;
    }
    vector<int> preXor(n+1);
    for(int i=1;i<=n;i++){
        preXor[i]=(preXor[i-1]^i);
    }
    for(int mod=2;mod<=n;mod++){
        int group=n/mod;
        int rest=n%mod;
        res^=preXor[rest];
        if(group%2==1){
            res^=preXor[mod-1];
        }
    }
    cout<<res;

}


int main(){
    solve();
}


Comments

Submit
0 Comments
More Questions

940C - Phone Numbers
1701C - Schedule Management
940D - Alena And The Heater
1747B - BAN BAN
1747A - Two Groups
1747C - Swap Game
1183D - Candy Box (easy version)
777B - Game of Credit Cards
1362A - Johnny and Ancient Computer
900C - Remove Extra One
328A - IQ Test
290B - QR code
290A - Mysterious strings
1593E - Gardener and Tree
290D - Orange
1027B - Numbers on the Chessboard
813A - The Contest
1560E - Polycarp and String Transformation
182D - Common Divisors
145A - Lucky Conversion
46B - T-shirts from Sponsor
545E - Paths and Trees
146B - Lucky Mask
1257E - The Contest
932C - Permutation Cycle
1423K - Lonely Numbers
831C - Jury Marks
94B - Friends
443B - Kolya and Tandem Repeat
582B - Once Again