牛客第四场题解

K NIO's Sword

玩家初始有一把攻击力为 0 的剑,需要依次击杀 n(1≤n≤10^6) 个敌人,仅当攻击力模 n 与 i 同余才能击杀第 i 个敌人。玩家可以升级剑,每次升级相当于在当前攻击力后面添加一个数字,问最少需要几次升级。

对每个敌人枚举需要升级的次数,初始值为上一个敌人的生命值。注意:当n=1时答案为0

#include<bits/stdc++.h>
using namespace std;
template <typename tn>void read(tn &n){
    tn f=1,t=0;char ch=getchar();
    while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
    while (isdigit(ch)) t=t*10+ch-'0',ch=getchar();
    n=f*t;
}
inline void out(int x){
	if(x>9)out(x/10);
	putchar(x%10+'0');
}
int n,Ans,wei,A,need;
int calc(int x){
	int sum=0;
	for(int i=1;i<=x;++i)sum=sum*10+9;
	return sum; 
}
int main(){
	read(n);
    if(n==1){
        cout<<"0"<<endl;
        return 0;
    }
	Ans=1;
	for(int i=2;i<=n;++i){
		A=i-1;
		wei=1;
		while(1){
			A=A*10%n;
			if(A==0)A=n;
			Ans++;
			if(i==n)need=n-A;
			else need=(n-A)+i;
			if(need<=calc(wei))break;
			wei++;
		}
	}
	cout<<Ans<<endl;
	return 0;
}

N Particle Arts

n 个粒子,每个粒子有一个能量 ai。两两随机碰撞,每碰撞一次两粒子的能量变为a&b,a∣b。
求所有粒子能量稳定后的方差最大值。n≤10^5,0≤ai<2^15

每次碰撞对粒子能量和没有影响,所以整体数字相差最大时即为所求

#include<bits/stdc++.h>
#define LL unsigned long long
using namespace std;
template <typename tn>void read(tn &n){
    tn f=1,t=0;char ch=getchar();
    while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
    while (isdigit(ch)) t=t*10+ch-'0',ch=getchar();
    n=f*t;
}
inline void out(int x){
	if(x>9)out(x/10);
	putchar(x%10+'0');
}
const int maxn = 1e5 + 5;
int n,a,bcnt[25];
LL sum,ea2;
LL gcd(LL a,LL b) {
	return b ? gcd(b, a % b) : a;
}
int main(){
	read(n);
	for(int i=1;i<=n;++i){
		read(a);
		sum+=a;
		for(int j=0,p=1;j<=15;++j,p<<=1)
			bcnt[j]+=((a&p)!=0);
	}
	LL tmp=0;
	for(int i=1;i<=n;++i){
		tmp=0;
		for(int j=0,p=1;j<=15;++j,p<<=1)
			if(i<=bcnt[j])tmp+=p;
		ea2+=tmp*tmp;
	}
	LL ans=n*ea2-sum*sum;
	LL div=1ull*n*n;
	LL d=gcd(ans,div);
	printf("%llu/%llu",ans/d,div/d);
	return 0;
}

D Jobs (Easy Version)

​ n个公司,每个公司有p个岗位,有对应的IQ、EQ、AQ要求,只要满足其中一个要求,即可获得offer。有q个朋友,问分别受到几个offer。

dp

#include <bits/stdc++.h>
using namespace std;
template <typename tn>void read(tn &n){
    tn f=1,t=0;char ch=getchar();
    while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
    while (isdigit(ch)) t=t*10+ch-'0',ch=getchar();
    n=f*t;
}
inline void out(int x){
	if(x>9)out(x/10);
	putchar(x%10+'0');
}
const int P=998244353;
int add(int a,int b){a+=b;return a<P?a:a-P;}
int mul(int a,int b){return 1ll*a*b%P;}
const int M=401;
short g[M][M][M],pc[1<<10];
int main(void){
    for(int i=1;i<(1<<10);++i)pc[i]=pc[i>>1]+(i&1);
    int n,q; 
	read(n),read(q);
    for(int i=0;i<n;++i){
        int k; 
		read(k);
        while(k--){
            int a,b,c;
            read(a),read(b),read(c);
            g[a][b][c]|=(1<<i);
        }
    } 
    for(int i=0;i+1<M;++i)
    for(int j=0;j<M;++j)
    for(int k=0;k<M;++k)
    g[i+1][j][k]|=g[i][j][k];
    for(int j=0;j+1<M;++j)
    for(int i=0;i<M;++i)
    for(int k=0;k<M;++k)
    g[i][j+1][k]|=g[i][j][k];
    for(int k=0;k+1<M;++k)
    for(int i=0;i<M;++i)
    for(int j=0;j<M;++j)
    g[i][j][k+1]|=g[i][j][k];
    int seed,lastans=0;
    read(seed);
    std::mt19937 rng(seed);
    std::uniform_int_distribution<> u(1,400);
    int ans=0,cnt=0;
    while (q--) {
        int IQ=(u(rng)^lastans)%400+1;  // The IQ of the i-th friend
        int EQ=(u(rng)^lastans)%400+1;  // The EQ of the i-th friend
        int AQ=(u(rng)^lastans)%400+1;  // The AQ of the i-th friend
        lastans=pc[g[IQ][EQ][AQ]];  // The answer to the i-th friend
        // printf("%d %d %d %d\n", IQ, EQ, AQ, lastans);
        ans = add(mul(ans, seed), lastans);
        // if (++cnt == 100) break;
    }
    printf("%d\n", ans);

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值