文章目录
2019 ICPC Asia Xuzhou Regional
A. Cat
题意: 给出一个区间[ l , r ] 区间内的数字是l , l + 1 , l + 2 , . . . , r ,让你找到一个最大的区间使得区间异或和小于s .
题解: 发现以偶数开头的连续4个可以异或完为0,因此对于R来说,如果R是奇数,那么R往前4的倍数;如果R是偶数,那么R–,然后往前4的倍数。最后剩下的暴力枚举。
代码:
#include <bits/stdc++.h>
typedef long long LL;
using namespace std;
LL check(LL l, LL r) {
if (l > r) return 0;
LL sum = 0;
if (r - l + 1 <= 10) {
for (LL i = l; i <= r; i++) sum ^= i;
return sum;
} else {
while (l % 4 != 0) sum ^= l, l++;
while (r % 4 != 3) sum ^= r, r--;
return sum;
}
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
LL l, r, s;
scanf("%lld%lld%lld", &l, &r, &s);
LL ans = -1;
for (int i = 0; i <= 3; i++) {
for (int j = 0; j <= 3; j++) {
LL L = l + i, R = r - j;
if (check(L, R) <= s) ans = max(ans, R - L + 1);
}
}
printf("%lld\n", ans == 0 ? -1 : ans);
}
return 0;
}
C. ❤️ numbers
题意:
题解: 打表题
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int L, R;
bool prime(int x) {
for (int i = 2; 1ll * i * i <= x; ++i) {
if (x % i == 0)
return false;
}
return true;
}
int main() {
int T; scanf("%d", &T);
while (T--) {
scanf("%d%d", &L, &R);
if (R - L + 1 > 60) {
puts("Yes");
} else {
int tot = R - L + 1;
int p = 0;
for (int i = L; i <= R; ++i) {
if (prime(i)) ++p;
}
puts(p * 3 < tot ? "Yes" : "No");
}
}
return 0;
}
E. Multiply
题意: 给定 Z = a 1 ! ∗ a 2 ! ∗ . . . ∗ a n ! Z = a_1! * a_2! * ... * a_n! Z=a1!∗a2!∗...∗an!,给定关系 b i = Z ∗ X i b_i = Z * X^i bi=Z∗Xi和X、Y,要求找到最小的下标i,使得 b i ∣ Y ! b_i | Y! bi∣Y!
题解: 本题没有告诉你无解打印啥,推测必然有解,那么答案大于等0。那么如果Z|Y,则输出0;否则,将X做质因子分解,然后只需要计算上写的阶乘内这些质因子有多少次幂。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int const MAXN = 1e5 + 10;
int T;
LL n, x, y, a[MAXN];
const int S=20;//随机算法判定次数,S越大,判错概率越小
//计算 (a*b)%c. a,b都是LL的数,直接相乘可能溢出的
// a,b,c <2^63
LL mult_mod(LL a,LL b,LL c) {
a%=c;
b%=c;
LL ret=0;
while(b) {
if(b&1){
ret+=a;ret%=c;}
a<<=1;
if(a>=c)a%=c;
b>>=1;
}
return ret;
}
//计算 x^n %c
LL pow_mod(LL x,LL n,LL mod) {
if(n==1)return x%mod;
x%=mod;
LL tmp=x;
LL ret=1;
while(n) {
if(n&1) ret=mult_mod(ret,tmp,mod);
tmp=mult_mod(tmp,tmp,mod);
n>>=1;
}
return ret;
}
//以a为基,n-1=x*2^t a^(n-1)=1(mod n) 验证n是不是合数
//一定是合数返回true,不一定返回false
bool check(LL a,LL n,LL x,LL t) {
LL ret=pow_mod(a,x,n);
LL last=ret;
for(int i=1;i<=t;i++) {
ret=mult_mod(ret,ret,n);
if(ret==1&&last!=1&&last!=n-1) return true;//合数
last=ret;
}
if(ret!=1) return true;
return false;
}
// Miller_Rabin()算法素数判定
//是素数返回true.(可能是伪素数,但概率极小)
//合数返回false;
bool Miller_Rabin(LL n) {
if(n<2)return false;
if(n==2)return true;
if((n&1)==0) return false;//偶数
LL x=n-1;
LL t=0;
while((x&1)==0){
x>>=1;t++;}
for(int i=0;i<S;i++) {
LL a=rand()%(n-1)+1;//rand()需要stdlib.h头文件
if(check(a,n,x,t))
return false;//合数
}
return true;
}
//************************************************
//pollard_rho 算法进行质因数分解
//************************************************
LL factor[MAXN];//质因数分解结果(刚返回时是无序的)
int tol;//质因数的个数。数组小标从0开始
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,k=2;
LL x0=rand()%x;
LL y=x0;
while(1) {
i++;
x0=(mult_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;}
}
}
//对n进行素因子分解
void findfac(LL n) {
if(Miller_Rabin(n)) {
//素数
factor[tol++]=n;
return;
}
LL p=n;
while(p>=n)p=Pollard_rho(p,rand()%(n-1)+1);
findfac