牛客练习赛48B题

1 篇文章 0 订阅

传送门

  • hash解法

这一题用hash解非常简单,为了防止hash冲突,可以使用多重hash,这里用了双层。

不会hash的同学可以去这里学一学:hash学习

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e5 + 10, N = 1e5;
const int mod1 = 1e9 + 7, mod2 = 998244353;
ll p1[maxn], p2[maxn];
int main() {
	ios::sync_with_stdio(false);
    p1[0] = p2[0] = 1;
    for (int i = 1; i <= N; i++) { 
        p1[i] = p1[i - 1] * i % mod1;//对阶乘取mod 
        p2[i] = p2[i - 1] * i % mod2;
        //一个数具有两个hash值,作为辨识它的密钥 
    }
    int T;
    cin>>T;
    while (T--) {
        int n, m, x;
        ll s1 = 1, s2 = 1;
        cin>>n>>m;
        for (int i = 1; i <= n; i++) {
            cin>>x;
            s1 *= p1[x]; s1 %= mod1;
            s2 *= p2[x]; s2 %= mod2;
        }
        ll s3 = 1, s4 = 1;
        for (int i = 1; i <= m; i++) {
            cin>>x;
            s3 *= p1[x]; s3 %= mod1;
            s4 *= p2[x]; s4 %= mod2;
        }
        if (s1 == s3 && s2 == s4)
            puts("equal");
        else
            puts("unequal");
    }
}
  • 分解因子解法

思路:首先用差分数组记录a,b数组的因子,求一遍前缀和相同的就会抵消掉,然后在对剩下的因子进行唯一分解,最后查看因子是否全部被抵消掉,抵消掉就相等,反之不相等。

还是hash方便呢TVT,不过打codeforces的时候建议大家尽量不要用hash,因为会被hack TAT。

#include<bits/stdc++.h>
using namespace std;

int const N=1e5+100;

int fact[N],cnt[N],p[N],tot=0;
bool vis[N];

void prime(int n){//素数筛 
	int m=(int)sqrt(n+0.5);	
	memset(vis,0,sizeof(vis));
	for(int i=2;i<n;i++)if(!vis[i])
		for(int j=i*i;j<n;j+=i)vis[j]=1;
	for(int i=2;i<n;i++)if(!vis[i])p[tot++]=i;	
} 	

void cal(int n,int flag){//分解因子 
	int temp;
	for(int i=0;i<tot&&p[i]*p[i]<=n;i++){
		temp=0;
		if(n%p[i]==0){
			while(n%p[i]==0)n/=p[i],temp++;
			cnt[i]+=temp*flag;
		}	 
	}
	if(n>1){
		int pos=lower_bound(p,p+tot,n)-p;
		cnt[pos]+=flag;
	}
} 

int main(){
	prime(N);
	int m,n,t,k;	
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&m);
		memset(cnt,0,sizeof(cnt));
		memset(fact,0,sizeof(fact));
		for(int i=0;i<n;i++){
			scanf("%d",&k);	
			fact[1]++,fact[k+1]--;//差分数组 
		}
		for(int i=0;i<m;i++){
			scanf("%d",&k);
			fact[1]--,fact[k+1]++;
		}
		for(int i=1;i<N;i++)fact[i]+=fact[i-1];//前缀和 
		for(int i=2;i<N;i++)
			if(fact[i]!=0)
				cal(i,fact[i]);	//计算剩余因子的分解	
		bool flag=1;		
		for(int i=0;i<tot;i++)
			if(cnt[i])flag=0;	
		if(flag)puts("equal");
		else puts("unequal");
	}
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值