牛客寒假算法基础集训营5 J 炫酷数学(打表找结论&快速幂)

  • 题目描述
    小希最近想知道一个东西,就是A+B=A|B(其中|为按位或)的二元组有多少个。
    当然,直接做这个式子对小希来说太难了,所以小希改变了一些条件,她仅想知道其中A,B<N的情况,其中N为2的幂次。
    当然,(A=1,B=0)和(A=0,B=1)被认为是不同的二元组。
  • 输入描述:
    第一行输入一个非负整数M。
    N=2M,M≤100
    即2的M次为N。
  • 输出描述:
    一个整数ans,对998244353取模。
  • 输入
    0
  • 输出
    1
  • 输入
    71
  • 输出
    588378066

做题几乎不用打表,看来打表也是很有用的,合理利用编译器.

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#define mod 998244353
using namespace std;
 
typedef long long LL;
const int Max_n=100005;

LL qpow(LL a,LL b){
	LL ans=1,res=a;
	while(b){
		if(b&1) ans*=res;
		res*=res;
		b>>=1;
	}
	return ans;
} 
int main(){
   	for(int i=0;i<=10;i++){
   		LL cnt=0;
   		LL n=qpow(2,i);
   		for(LL j=0;j<n;j++){
   			for(LL k=0;k<n;k++){
   				if(j+k==(j|k))
   					cnt++;
   			}
   		}
   		printf("%lld ",cnt%mod);
   	}
    return 0;
}

在这里插入图片描述
规律上图所示,我们可以看出答案就是3^m%mod

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#define mod 998244353
using namespace std;
 
typedef long long LL;
const int Max_n=100005;

LL qpow(LL a,LL b){
	LL ans=1,res=a;
	while(b){
		if(b&1) ans=(ans*res)%mod;
		res=(res*res)%mod;
		b>>=1;
	}
	return ans;
} 
int main(){
   	int m;
   	scanf("%d",&m);
   	LL ans=qpow(3,m);
   	printf("%lld\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值