A - Cat
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <map>
#include <list>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <stack>
#include <set>
#pragma GCC optimize(3 , "Ofast" , "inline")
using namespace std ;
#define ios ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#define x first
#define y second
typedef long long ll ;
const double esp = 1e-6 , pi = acos(-1) ;
typedef pair<int , int> PII ;
const int N = 1e6 + 10 , INF = 0x3f3f3f3f , mod = 1e9 + 7;
ll in()
{
ll x = 0 , f = 1 ;
char ch = getchar() ;
while(!isdigit(ch)) {if(ch == '-') f = -1 ; ch = getchar() ;}
while(isdigit(ch)) x = x * 10 + ch - 48 , ch = getchar() ;
return x * f ;
}
void work()
{
ll l = in() , r = in() , s = in() ;
if(r - l + 1 <= 4) {
ll ans = -1 ;
for(ll i = l ;i <= r ;i ++ )
{
ll res = 0 ;
for(ll j = i ;j <= r ;j ++ ) {
res ^= j ;
if(res <= s) ans = max(ans , j - i + 1) ;
}
}
cout << ans << endl ;
return ;
}
if(l % 2) {
ll rr ;
for(ll i = r; i ;i --)
{
if((i - l) % 4 == 0)
{
rr = i ;
break ;
}
}
// cout << rr << endl ;
ll ans = rr - l ;
if(l <= s) {
ans ++ ;
}
ll res = 0 ;
for(ll i = rr + 1 ; i <= r ;i ++ ) {
res ^= i ;
if(res <= s) {
ans = max(ans , i - l) ;
}
if((res ^ l) <= s) {
ans = max(ans , i - l + 1) ;
}
// cout << i << " " << res << " " << (res ^ l) << " " << ans << endl ;
}
cout << ans << endl ;
}
else {
for(ll i = r ; ;i --) {
if((i - l + 1) % 4 == 0) {
ll ans = i - l + 1 ;
ll res = 0 ;
for(ll j = i + 1; j <= r ;j ++ ) {
res ^= j ;
if(res <= s)
ans = max(ans , j - l + 1) ;
}
cout << ans << endl ;
return ;
}
}
}
return ;
}
int main()
{
int t = in() ;
while(t --) work() ;
return 0 ;
}
/*
*/
C - ❤️ numbers
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <map>
#include <list>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <stack>
#include <set>
#pragma GCC optimize(3 , "Ofast" , "inline")
using namespace std ;
#define ios ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#define x first
#define y second
typedef long long ll ;
const double esp = 1e-6 , pi = acos(-1) ;
typedef pair<int , int> PII ;
const int N = 1e6 + 10 , INF = 0x3f3f3f3f , mod = 1e9 + 7;
ll in()
{
ll x = 0 , f = 1 ;
char ch = getchar() ;
while(!isdigit(ch)) {if(ch == '-') f = -1 ; ch = getchar() ;}
while(isdigit(ch)) x = x * 10 + ch - 48 , ch = getchar() ;
return x * f ;
}
int prime[N] , vis[N] ;
void work()
{
int l = in() , r = in() ;
if(r - l + 1 >= 10000) puts("Yes") ;
else {
int tot = 0 ;
for(int i = 2; i < N ;i ++ ) {
if(!vis[i]) prime[++ tot] = i ;
for(int j = 1; j <= tot && prime[j] * i < N ;j ++ ) {
vis[i * prime[j]] = 1 ;
if(i % prime[j] == 0) break ;
}
}
int ans = 0 ;
for(int i = l ;i <= r;i ++ ) {
if(i < N && !vis[i]) ans ++ ;
else if(i >= N) {
int f = 1;
for(int j = 1; j <= tot ;j ++ ) {
if(i % prime[j] == 0) {
f = 0 ;
break ;
}
}
if(f) ans ++ ;
}
}
if(ans * 3 < r - l + 1) puts("Yes") ;
else puts("No") ;
}
return ;
}
int main()
{
int n = in() ;
while(n --) work() ;
return 0 ;
}
/*
*/
E - Multiply
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <algorithm>
#include <iostream>
const int S=20;
using namespace std;
typedef long long ll;
#define maxn 1000000
ll factor[maxn];
int tot;
ll muti_mod(ll a,ll b,ll c){ //返回(a*b) mod c,a,b,c<2^63
a%=c;
b%=c;
ll ret=0;
while (b){
if (b&1){
ret+=a;
if (ret>=c) ret-=c;
}
a<<=1;
if (a>=c) a-=c;
b>>=1;
}
return ret;
}
ll pow_mod(ll x,ll n,ll mod){ //返回x^n mod c ,非递归版
if (n==1) return x%mod;
int bit[64],k=0;
while (n){
bit[k++]=n&1;
n>>=1;
}
ll ret=1;
for (k=k-1;k>=0;k--){
ret=muti_mod(ret,ret,mod);
if (bit[k]==1) ret=muti_mod(ret,x,mod);
}
return ret;
}
bool check(ll a,ll n,ll x,ll t){ //以a为基,n-1=x*2^t,检验n是不是合数
ll ret=pow_mod(a,x,n),last=ret;
for (int i=1;i<=t;i++){
ret=muti_mod(ret,ret,n);
if (ret==1 && last!=1 && last!=n-1) return 1;
last=ret;
}
if (ret!=1) return 1;
return 0;
}
bool Miller_Rabin(ll n){
ll x=n-1,t=0;
while ((x&1)==0) x>>=1,t++;
bool flag=1;
if (t>=1 && (x&1)==1){
for (int k=0;k<S;k++){
ll a=rand()%(n-1)+1;
if (check(a,n,x,t)) {flag=1;break;}
flag=0;
}
}
if (!flag || n==2) return 0;
return 1;
}
ll gcd(ll a,ll b){
if (a==0) return 1;
if (a<0) return gcd(-a,b);
while (b){
ll t=a%b; a=b; b=t;
}
return a;
}
ll Pollard_rho(ll x,ll c){
ll i=1,x0=rand()%x,y=x0,k=2;
while (1){
i++;
x0=(muti_mod(x0,x0,x)+c)%x;
ll d=gcd(y-x0,x);
if (d!=1 && d!=x){
return d;
}
if (y==x0) return x;
if (i==k){
y=x0;
k+=k;
}
}
}
void findfac(ll n){ //递归进行质因数分解N
if (!Miller_Rabin(n)){
factor[tot++] = n;
return;
}
ll p=n;
while (p>=n) p=Pollard_rho(p,rand() % (n-1) +1);
findfac(p);
findfac(n/p);
}
int idx = 0 ;
struct node
{
ll val , num ;
node() {}
node(ll val , ll num) : val(val) , num(num) {}
bool operator<(const node &a) const
{
val < a.val ;
}
}fac[maxn];
ll b[maxn] ;
bool solve(ll n)
{
if (!Miller_Rabin(n))
{
fac[++ idx] = {n , 1} ;
}
else{
tot = 0;
findfac(n);
sort(factor , factor + tot) ;
idx = 0 ;
for(int i = 0 ; i < tot ;i ++)
{
if(factor[i] != fac[idx].val)
fac[++ idx] = {factor[i] , 1};
else
fac[idx].num ++ ;
}
}
}
ll get(ll n , ll p)
{
ll res = 0 ;
while(n)
{
res += n / p ;
n /= p ;
}
return res ;
}
int main(){
srand(time(NULL));
int t;
scanf("%d",&t);
while (t--){
ll n , x , y ;
scanf("%lld%lld%lld",&n , &x , &y);
idx = 0 ;
solve(x) ;
for(int j = 1; j <= idx ;j ++) b[j] = 0 ;
for(int i = 1; i <= n ;i ++)
{
ll p ;
scanf("%lld" , &p) ;
for(int j = 1 ;j <= idx ;j ++)
b[j] += get(p , fac[j].val) ;
}
ll minx = 0 ;
for(int j = 1; j <= idx ;j ++)
b[j] = get(y , fac[j].val) - b[j] , minx = max(minx , b[j]);
for(int j = 1; j <= idx ;j ++)
if(b[j] > 0)
minx = min(minx , b[j] / fac[j].num) ;
printf("%lld
" , minx) ;
}
}
F - The Answer to the Ultimate Question of Life, The Universe, and Everything.
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <map>
#include <list>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <stack>
#include <set>
#pragma GCC optimize(3 , "Ofast" , "inline")
using namespace std ;
#define ios ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#define x first
#define y second
typedef long long ll ;
const double esp = 1e-6 , pi = acos(-1) ;
typedef pair<int , int> PII ;
const int N = 1e6 + 10 , INF = 0x3f3f3f3f , mod = 1e9 + 7;
ll in()
{
ll x = 0 , f = 1 ;
char ch = getchar() ;
while(!isdigit(ch)) {if(ch == '-') f = -1 ; ch = getchar() ;}
while(isdigit(ch)) x = x * 10 + ch - 48 , ch = getchar() ;
return x * f ;
}
int a[210][3] = {
-5000 , 0 , 5000,
-5000 , 1 , 5000,
-4373 , -486 , 4375,
-5 , 4 , 4,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
-637 , -205 , 644,
-169 , 44 , 168,
-5000 , 2 , 5000,
-216 , -52 , 217,
-650 , -353 , 683,
-695 , -641 , 843,
-11 , 7 , 10,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
-265 , -262 , 332,
-4114 , -588 , 4118,
-3331 , 2195 , 2977,
-1373 , -1276 , 1671,
-95 , 47 , 91,
-2816 , -741 , 2833,
-401 , -287 , 445,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
-10 , 8 , 8,
-2683 , 1839 , 2357,
-2107 , 237 , 2106,
-5000 , 3 , 5000,
-2268 , -249 , 2269,
-233 , -69 , 235,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
-1555 , -244 , 1557,
-1120 , -509 , 1154,
-3223 , 2358 , 2731,
-444 , -84 , 445,
-27 , 16 , 25,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
-823 , -307 , 837,
-7 , -5 , 8,
-2369 , 1709 , 2025,
-758 , -473 , 815,
-141 , 49 , 139,
-3950 , -1247 , 3991,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
-796 , 602 , 659,
1061109567 , 1061109567 , 1061109567 ,
-2370 , 1518 , 2141,
-3885 , -648 , 3891,
-3329 , 1837 , 3131,
-672 , 505 , 559,
-998 , 361 , 982,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
-1201 , -163 , 1202,
-966 , 668 , 845,
-2744 , -1561 , 2903,
-161 , 102 , 146,
-5000 , 4 , 5000,
-929 , 403 , 903,
1 , 1 , 4,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
-403 , 134 , 398,
-2359 , 824 , 2325,
-533 , 401 , 443,
-432 , -104 , 434,
-335 , -146 , 344,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
-2080 , -829 , 2123,
-706 , -196 , 711,
-1300 , -706 , 1366,
-2368 , -1719 , 2638,
-1317 , 847 , 1188,
-3707 , 1315 , 3651,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
-4126 , -1972 , 4271,
-1390 , -1282 , 1686,
-2514 , 1953 , 2036,
-1803 , 365 , 1798,
-3389 , -2912 , 3992,
-4052 , 861 , 4039,
-248 , -98 , 253,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
-22 , 14 , 20,
-3168 , -991 , 3200,
-2101 , -1638 , 2391,
-893 , -622 , 984,
-1797 , -903 , 1870,
-2327 , 319 , 2325,
-239 , 118 , 229,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
-7 , -4 , 8,
-2689 , -1165 , 2760,
-1309 , 947 , 1117,
-1165 , -948 , 1345,
-2948 , 853 , 2924,
1061109567 , 1061109567 , 1061109567 ,
-4793 , -2312 , 4966,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
-12 , 8 , 11,
-1906 , -757 , 1945,
-896 , -555 , 962,
-4328 , 383 , 4327,
-3677 , -1673 , 3789,
-2804 , 1219 , 2725,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
-37 , -16 , 38,
-1 , 0 , 5,
-5000 , 5 , 5000,
-2212 , -419 , 2217,
-4034 , -3881 , 4988,
-3989 , -726 , 3997,
-1580 , -1238 , 1801,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
-1 , 2 , 5,
-399 , 167 , 389,
-3013 , -1766 , 3203,
-1351 , -629 , 1395,
-1116 , 816 , 946,
-758 , -428 , 801,
-86 , -77 , 103,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
-139 , 104 , 116,
-7 , -3 , 8,
1061109567 , 1061109567 , 1061109567 ,
-2746 , -2552 , 3342,
-8 , -7 , 10,
-327 , -263 , 376,
-2366 , 1528 , 2131,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
-367 , 260 , 317,
-463 , 215 , 447,
-805 , 486 , 741,
-3736 , -695 , 3744,
-2135 , -516 , 2145,
-3693 , -1049 , 3721,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
-1534 , 383 , 1526,
-3874 , -1654 , 3972,
-4767 , -2476 , 4980,
-4125 , -1417 , 4180,
-3423 , -2943 , 4033,
-66 , -59 , 79,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
-802 , -574 , 890,
-1354 , -1012 , 1521,
-3834 , -2149 , 4047,
-1328 , 891 , 1178,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
-335 , -170 , 349,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
-1168 , -160 , 1169,
-13 , -10 , 15,
-2839 , 1503 , 2691,
1061109567 , 1061109567 , 1061109567 ,
-4874 , 974 , 4861,
-90 , -29 , 91,
-4889 , 976 , 4876,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
-4 , 5 , 5,
-1885 , -1092 , 2000,
-1639 , 318 , 1635,
-1702 , -1403 , 1974,
-4812 , -593 , 4815,
-377 , -215 , 399,
-20 , 16 , 16,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
1061109567 , 1061109567 , 1061109567 ,
-1057 , -579 , 1112,
-2867 , -1606 , 3026,
-3752 , -1347 , 3809,
-2208 , 508 , 2199,
-2318 , -638 , 2334,
};
int main()
{
int t = in() ;
while(t --)
{
int x = in() ;
if(a[x][0] != INF) cout << a[x][0] << " " << a[x][1] << " " << a[x][2] << endl ;
else puts("impossible") ;
}
return 0 ;
}
/*
*/
H - Yuuki and a problem
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <map>
#include <list>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <stack>
#include <set>
#pragma GCC optimize(3 , "Ofast" , "inline")
using namespace std ;
#define ios ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#define x first
#define y second
typedef long long ll ;
const double esp = 1e-6 , pi = acos(-1) ;
typedef pair<int , int> PII ;
const int N = 2e5 + 10 , INF = 0x3f3f3f3f , mod = 1e9 + 7;
ll in()
{
ll x = 0 , f = 1 ;
char ch = getchar() ;
while(!isdigit(ch)) {if(ch == '-') f = -1 ; ch = getchar() ;}
while(isdigit(ch)) x = x * 10 + ch - 48 , ch = getchar() ;
return x * f ;
}
int n , m = 2e5, q , a[N] , L[30][30] , R[30][30] , cl , cr ;
int lowbit(int x){return x & -x ;}
struct Seg{
struct node{
int ls , rs ;
ll sum ;
void init() {ls = rs = sum = 0 ;}
}t[N * 100] ;
int rt[N] , tot ;
ll res ;
int newnode(){
++ tot ;
t[tot].init() ;
return tot ;
}
void init() {memset(rt , 0 , sizeof rt) ; tot = 0 ;}
void update(int &rt , int l , int r , int pos , int v) {
if(!rt) rt = newnode() ;
t[rt].sum += v ;
if(l == r) return ;
int mid = l + r >> 1 ;
if(pos <= mid) update(t[rt].ls , l , mid , pos , v) ;
else update(t[rt].rs , mid + 1 , r , pos , v) ;
}
void update(int x , int pos , int v){
while(x <= n)
update(rt[x] , 1 , m , pos , v) , x += lowbit(x) ;
}
void query(int dep , int l , int r , int ql , int qr){
if(ql > qr) return ;
if(ql <= l && r <= qr) {
for(int i = 1; i <= cl ;i ++ ) res -= t[L[dep][i]].sum ;
for(int i = 1; i <= cr ;i ++ ) res += t[R[dep][i]].sum ;
return ;
}
int mid = l + r >> 1 ;
if(ql <= mid) {
for(int i = 1; i <= cl ;i ++ ) L[dep + 1][i] = t[L[dep][i]].ls ;
for(int i = 1; i <= cr ;i ++ ) R[dep + 1][i] = t[R[dep][i]].ls ;
query(dep + 1 , l , mid , ql , qr) ;
}
if(qr > mid) {
for(int i = 1; i <= cl ;i ++ ) L[dep + 1][i] = t[L[dep][i]].rs ;
for(int i = 1; i <= cr ;i ++ ) R[dep + 1][i] = t[R[dep][i]].rs ;
query(dep + 1 , mid + 1 , r , ql , qr) ;
}
}
}seg;
int main()
{
while(scanf("%d%d" , &n ,&q) != EOF) {
for(int i = 1;i <= n ;i ++ ) scanf("%d" , &a[i]) ;
seg.init() ;
for(int i = 1; i <= n ;i ++ )
seg.update(i , a[i] , a[i]) ;
int op , x , y ;
while(q --) {
scanf("%d%d%d" , &op , &x , &y) ;
if(op == 1) {
seg.update(x , a[x] , -a[x]) ;
a[x] = y ;
seg.update(x , a[x] , a[x]) ;
}else {
x -- ;
cl = 0 , cr = 0 ;
for(int j = x ;j ;j -= lowbit(j)) L[0][++ cl] = seg.rt[j] ;
for(int j = y ;j ;j -= lowbit(j)) R[0][++ cr] = seg.rt[j] ;
ll l = 0 , r = 0 ;
do{
r = l + 1 ;
seg.res = 0 ;
seg.query(0 , 1 , m , 1 , min(1ll * m , r)) ;
l = seg.res ;
}while(l >= r) ;
printf("%lld
" , l + 1) ;
}
}
}
return 0 ;
}
/*
*/
M - Kill the tree
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <map>
#include <list>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <stack>
#include <set>
#pragma GCC optimize(3 , "Ofast" , "inline")
using namespace std ;
#define ios ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#define x first
#define y second
#define pb push_back
typedef long long ll ;
const double esp = 1e-6 , pi = acos(-1) ;
typedef pair<int , int> PII ;
const int N = 1e6 + 10 , INF = 0x3f3f3f3f , mod = 1e9 + 7;
ll in()
{
ll x = 0 , f = 1 ;
char ch = getchar() ;
while(!isdigit(ch)) {if(ch == '-') f = -1 ; ch = getchar() ;}
while(isdigit(ch)) x = x * 10 + ch - 48 , ch = getchar() ;
return x * f ;
}
int n , size[N] , son[N] ;
vector<int> res[N] , v[N] ;
int fa[N] ;
void dfs(int u , int f){
size[u] = 1 ;
fa[u] = f ;
for(auto x : v[u]) {
if(x == f) continue ;
dfs(x , u) ;
size[u] += size[x] ;
if(size[x] > size[son[u]])
son[u] = x ;
}
if(size[u] == 1 || size[son[u]] < size[u] - size[son[u]]) {
res[u].pb(u) ;
return ;
}
for(auto x : res[son[u]]) {
while(x != u && size[u] - size[x] > size[x]) x = fa[x] ;
res[u].pb(x) ;
if(x != u && size[u] - size[x] == size[x]) res[u].pb(fa[x]) ;
}
sort(res[u].begin() , res[u].end()) ;
res[u].erase(unique(res[u].begin() , res[u].end()) , res[u].end()) ;
}
int main()
{
n = in() ;
for(int i = 1; i < n ;i ++ ) {
int a = in() , b = in() ;
v[a].pb(b) , v[b].pb(a) ;
}
dfs(1 , 0) ;
for(int i = 1; i <= n ;i ++ ) {
if(res[i].size() == 1)
cout << res[i][0] << endl ;
else cout << res[i][0] << " " << res[i][1] << endl ;
}
return 0 ;
}
/*
*/