Editorial for Đội mũ
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này
Code mẫu của ladpro98
#include <bits/stdc++.h> using namespace std; int t; long long n, p; long long POS(long long x) { long long mid, res = 0; while (x > 1) { mid = log2l((long double)(x-1)); x -= pow(2, mid); res++; } return res % p; } int main() { scanf("%d", &t); while (t--) { scanf("%lld %lld", &n, &p); printf("%lld\n", POS(n), n, p); } return 0; }
Code mẫu của skyvn97
#include<bits/stdc++.h> #define REP(i,n) for (int i=0,_n=(n);i<_n;i=i+1) #define BIT(x,i) (((x)>>(i))&1) using namespace std; typedef long long ll; void process(ll n,ll p) { int res=0; REP(i,63) if (BIT(n-1,i)) res++; cout<<res%p<<"\n"; } int main(void) { ios::sync_with_stdio(false);cin.tie(NULL); int t; cin>>t; ll n,p; while (cin>>n>>p) process(n,p); return 0; }
Comments