跳到正文

题解

题解:P15790 「1&0OI R1」相思若循

huangzhirui2013约 1 分钟
原文
文章信息与版本

文章信息

发布
2026年3月14日
收录
2026年9月1日
更新
2026年11月15日
篇幅
约 1 分钟
题目
P15790

版本

  1. v1当前

本布丁观察样例发现本题为快速幂

C++
#include<bits/stdc++.h>
using namespace std;
long long n,t,i,a[500010],mod=998244353;

void ab(long long aa,long long b)//快速幂模版
{
	if(b==1)
	{
		cout<<"NO"<<std::endl;
		return;
	}
	long long ret=1;
	while(b){
		if(b%2==1)ret=ret*aa%mod;
		aa=aa*aa%mod;
		b=b/2;      
	}
	if(ret==0)
	{
		cout<<mod-1<<std::endl;
	}
	else
	{
		cout<<ret-1<<std::endl;
	}
	return;
}
int main()
{
	cin>>t;
	for(i=1;i<=t;i++)
		cin>>a[i];
	for(i=1;i<=t;i++)
		ab(2,a[i]);
	return 0;//结束撒花
}

总结

代码
核心算法:使用快速幂高效计算 2nmod998244353,避免超时和溢出
题目要求:计算 (2n−1)mod998244353,且当 n=1 时输出 "NO"
关键优化:快速幂的时间复杂度为 O(logn),是处理大指数幂运算的标准方法

这份代码的核心就是快速幂的应用,理解了快速幂的原理,就能完全掌握这道题的解法。 第一篇题解欢迎评论

原文