场上四题,1001,1007,1008,1010
1001:AND Minimum Spanning Tree
贪心,选择数字i的最低位0,设它为第j位,那么它的父亲就是(1<
#include
#define ll long long
using namespace std;
const int maxn = 2e5 + 50;
int ans[maxn];
int main()
{
int T;cin>>T;
int n;
while(T--){
scanf("%d", &n);
ll sum = 0;
for(int i = 2; i <= n; ++i){
int ok = 0;
int j;
for(j = 0; (1< 2) printf(" ");
printf("%d", ans[i]);
}printf("\n");
}
}
1007:Just an Old Puzzle
4*4的数字华容道,前人总结出了结论:只要有解,那么它一定可以在80步以内解出来。怎么判断有解?如果 “逆序对个数” 和 “当前0所在行和目标的0所在的行的差距”的奇偶性相同,则有解。
#include
#define ull unsigned long long
using namespace std;
int a[16];
int main(){
int T;
scanf("%d",&T);
while(T--){
int h;
int pos = 0;
for(int i = 0; i < 16; ++i){
int x;
scanf("%d", &x);
if(x == 0) h = i/4;//第几行
else a[pos++] = x;
}
int num = 0;
for(int i = 1; i < pos; ++i){
for(int j = 0; j < i; ++j){
if(a[j] > a[i]) num++;
}
}
if(h&1){
if(num&1) printf("No\n");
else printf("Yes\n");
}
else{
if(num&1) printf("Yes\n");
else printf("No\n");
}
}
}
1008:K-th Closest Distance
考虑二分答案。设check的值为lim,即判断第k小的距离为lim可不可行。
主席树判断[L,R]区间内,数值范围为[p-lim,p+lim]范围内的数字有多少个,如果大于等于k,则二分右端点左移,否则左端点右移。
#include
#define ll long long
#define mid ((l+r)>>1)
using namespace std;
const int maxn = 1e6 + 50;
int sz[maxn*20], lc[maxn*20], rc[maxn*20];
int tot;
int T[maxn];
int n, m;
void build(int pre, int &cur, int l, int r, int pos){
if(!cur){
cur = ++tot;
lc[cur] = rc[cur] = 0;
}
sz[cur] = sz[pre]+1;
if(l == r) return;
if(pos <= mid) {
rc[cur] = rc[pre];
build(lc[pre], lc[cur], l, mid, pos);
}
else{
lc[cur] = lc[pre];
build(rc[pre], rc[cur], mid+1, r, pos);
}
}
int query(int pre, int cur, int l, int r, int pos){//有几个<=pos的数字
//cout<<"l:"< r) {
return sz[cur] - sz[pre];
}
if(pos <= mid) return query(lc[pre], lc[cur], l, mid, pos);
else {
int t1 = query(lc[pre], lc[cur], l, mid, pos) ;
int t2 = query(rc[pre], rc[cur], mid+1, r, pos);
return t1+t2;
}
}
void init(){
T[0] = tot = 0;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i){
int x;
scanf("%d", &x);
T[i] = 0;
build(T[i-1], T[i], 1, maxn, x);
}
}
bool check(int lim, int p, int k, int L, int R){
int t1 = query(T[L-1], T[R], 1, maxn, p+lim) ;
int t2 = query(T[L-1], T[R], 1, maxn, p-lim-1);
int num = t1 - t2;
//cout<= k;
}
void sol()
{
int ans = 0;
while(m--){
int L, R, p, k;
scanf("%d%d%d%d", &L, &R, &p, &k);
L ^= ans; R ^= ans; p ^= ans; k ^= ans;
int l = 0, r = maxn;
while(l <= r){
if(check(mid, p, k, L, R)) ans = mid, r = mid - 1;
else l = mid + 1;
}
printf("%d\n", ans);
}
}
int main()
{
int ca;cin>>ca;
while(ca--){
init();sol();
}
}
1010:Minimal Power of Prime
先预处理出 n 1 5 n^{\frac{1}{5}} n51内的所有素数,用这些素数把n筛一遍。
如果筛完n不为1,那么n最多由4个质数相乘组成,有以下形式:
x , x 2 , x 3 , x 4 , x 3 ∗ y , x 2 ∗ y 2 , x 2 ∗ y , x ∗ y x,x^2,x^3,x^4,x^3*y,x^2*y^2,x^2*y,x*y x,x2,x3,x4,x3∗y,x2∗y2,x2∗y,x∗y
可以发现除了 x 2 , x 3 , x 4 , x 2 ∗ y 2 x^2,x^3,x^4,x^2*y^2 x2,x3,x4,x2∗y2之外,其他的答案都为1。那么只要判断剩下的n是这些情况中的哪一个就可以了。要用二分判断,sqrt和pow函数会有精度误差。
#include
#include
#include
#include
#include
#include
#define ll long long
using namespace std;
const int maxn = 1e4 + 50;
int he[maxn];
int pp[maxn];
int cnt;
bool f3(ll n){
ll l = 1, r = 1e6 + 3;
while(l <= r){
ll mid = (l+r)>>1;
if(mid*mid*mid == n) return true;
else if(mid*mid*mid < n) l = mid+1;
else r = mid - 1;
}return false;
}
bool f4(ll n)
{
ll l = 1, r = 31628;
while(l <= r){
ll mid = (l+r)>>1;
ll temp = mid*mid*mid*mid;
if(temp == n) return true;
else if(temp < n) l = mid + 1;
else r = mid-1;
}return false;
}
int main(){
// freopen("1.in", "r", stdin);
// freopen("1.out", "w", stdout);
for(int i = 4; i < maxn; i+= 2) he[i] = 1;
for(int i = 3; i*i < maxn; i += 2){
if(!he[i])
for(int j = i*i; j < maxn; j += 2*i) he[j] = 1;
}
cnt = 0;
for(int i = 2; i < maxn; ++i) if(!he[i]) pp[cnt++] = i;
int T;cin>>T;
while(T--){
ll n;
scanf("%lld", &n);
int ans = 0x3f3f3f3f;
for(int i = 0; i < cnt; ++i){
int tt = 0;
while(n%pp[i] == 0) tt++, n/=pp[i];
if(tt > 0) ans = min(ans, tt);
if(ans == 1) break;
}
if(ans == 1){
printf("1\n");continue;
}
if(n == 1){
printf("%d\n", ans);continue;
}
ll t = sqrt(n);
int is4 = f4(n), is2 = (t*t == n), is3 = f3(n);
if(is4){//是四次方
ans = min(ans, 4);
}
if(!is4 && is2){
ans = min(ans, 2);
}
if(is3){
ans = min(ans, 3), is3 = 1;
}
if(!is4 && !is2 && !is3) ans = 1;
printf("%d\n", ans);
}
return 0;
}
/*
5
1000009000027000027
*/