本文涉及知识点
数论:质数、最大公约数、菲蜀定理
快速指数幂
P9915 「RiOI-03」3-2
题目背景
Heart beat to death.
题目描述
给定一个正整数 n n n。将 [ 0 , 2 n ) [0,2^n) [0,2n) 中每个整数的二进制最低 n n n 位从低到高依次写在一个 2 n × n 2^n\times n 2n×n 的矩阵上。矩阵两维的下标都从 0 0 0 开始。 如,当 n = 3 n=3 n=3 时,矩阵是这样的:
给定 q q q 次询问,每次询问这个矩阵下标为 ( x , y ) (x,y) (x,y) 的格子所在的四连通块大小对 998244353 998244353 998244353 取模的值。
输入格式
第一行两个正整数 n , q n,q n,q。
接下来 q q q 行,每行两个非负整数 x , y x,y x,y,表示一次询问。
输出格式
输出 q q q 行,每行一个正整数,表示每次询问答案对 998244353 998244353 998244353 取模的值。
输入输出样例 #1
输入 #1
2 2
1 1
2 0
输出 #1
3
1
说明/提示
【样例 #1 解释】
图为 n = 2 n=2 n=2 时的矩阵,其中同一个颜色的为一个四连通块。
【数据范围】
本题开启捆绑测试。
SubTask \text{SubTask} SubTask | 分值 | n ≤ n\le n≤ | q ≤ q\le q≤ |
---|---|---|---|
0 0 0 | 5 5 5 | 15 15 15 | 15 15 15 |
1 1 1 | 20 20 20 | 15 15 15 | 5 × 1 0 5 5\times 10^5 5×105 |
2 2 2 | 25 25 25 | 5 × 1 0 3 5\times 10^3 5×103 | 5 × 1 0 3 5\times 10^3 5×103 |
3 3 3 | 50 50 50 | 1 0 18 10^{18} 1018 | 5 × 1 0 5 5\times 10^5 5×105 |
对于 100 % 100\% 100% 的数据, 0 ≤ y < n ≤ 1 0 18 0\le y\lt n\le 10^{18} 0≤y<n≤1018, 0 ≤ x < min ( 2 n , 1 0 18 ) 0\le x\lt \min(2^n, 10^{18}) 0≤x<min(2n,1018), 1 ≤ q ≤ 5 × 1 0 5 1\le q\le 5\times 10^5 1≤q≤5×105。
请选用较快的输入输出方式。
数论
第y列:以 2 y 为周期 2^y为周期 2y为周期,偶数周期为0,奇数周期为1,且所有周期都是完整的。
∀ ( x , y ) \forall(x,y) ∀(x,y)所在连通块在第i列的长度 2 i , i ≤ y 2^i,i \le y 2i,i≤y
如果 ( x , y + 1 ) 和 ( x , y ) 连通,则连通块第 y + 1 列的长度为 2 y + 1 ,否则为 0 。 (x,y+1)和(x,y)连通,则连通块第y+1列的长度为2^{y+1},否则为0。 (x,y+1)和(x,y)连通,则连通块第y+1列的长度为2y+1,否则为0。为0结束迭代,否则继续迭代。
求最小z, ( x , y ) ≠ ( x , z ) (x,y) \neq (x,z) (x,y)=(x,z)
结果为 ∑ i : 0 z − 1 2 i = 2 z − 1 \sum_{i:0}^{z-1}2^i=2^z-1 ∑i:0z−12i=2z−1
周期数奇偶性相同,则值相同。
列数最大 n = 1 0 18 n=10^{18} n=1018 ,但 z ≈ 60 z \approx 60 z≈60时 2 z ≥ x 2^z \ge x 2z≥x,即后面的列全部时0。要么全部是,要么全部不是。
时间复杂度:O(Qlogn)
实现
z = y;
当 (z < N )且((x,y)和(x,z))相同且 (x >= 2^z){z++};
if( N== z) 返回z。
((x,y)和(x,z))不同返回z
如果(x,y)是1,反回z。
返回N。
代码
核心代码
#include <iostream>
#include <sstream>
#include <vector>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<string>
#include<algorithm>
#include<functional>
#include<queue>
#include <stack>
#include<iomanip>
#include<numeric>
#include <math.h>
#include <climits>
#include<assert.h>
#include<cstring>
#include<list>
#include<array>
#include <bitset>
#include <chrono>
using namespace std::chrono;
using namespace std;
template<class T1, class T2>
std::istream& operator >> (std::istream& in, pair<T1, T2>& pr) {
in >> pr.first >> pr.second;
return in;
}
template<class T1, class T2, class T3 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t);
return in;
}
template<class T1, class T2, class T3, class T4 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t);
return in;
}
template<class T1, class T2, class T3, class T4, class T5 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4, T5>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t) ;
return in;
}
template<class T1, class T2, class T3, class T4, class T5, class T6 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4, T5, T6>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t) >> get<5>(t) ;
return in;
}
template<class T1, class T2, class T3, class T4, class T5, class T6, class T7 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4, T5, T6, T7>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t) >> get<5>(t) >> get<6>(t);
return in;
}
template<class T = int>
vector<T> Read() {
int n;
cin >> n;
vector<T> ret(n);
for (int i = 0; i < n; i++) {
cin >> ret[i];
}
return ret;
}
template<class T = int>
vector<T> ReadNotNum() {
vector<T> ret;
T tmp;
while (cin >> tmp) {
ret.emplace_back(tmp);
if ('\n' == cin.get()) { break; }
}
return ret;
}
template<class T = int>
vector<T> Read(int n) {
vector<T> ret(n);
for (int i = 0; i < n; i++) {
cin >> ret[i];
}
return ret;
}
template<int N = 1'000'000>
class COutBuff
{
public:
COutBuff() {
m_p = puffer;
}
template<class T>
void write(T x) {
int num[28], sp = 0;
if (x < 0)
*m_p++ = '-', x = -x;
if (!x)
*m_p++ = 48;
while (x)
num[++sp] = x % 10, x /= 10;
while (sp)
*m_p++ = num[sp--] + 48;
AuotToFile();
}
void writestr(const char* sz) {
strcpy(m_p, sz);
m_p += strlen(sz);
AuotToFile();
}
inline void write(char ch)
{
*m_p++ = ch;
AuotToFile();
}
inline void ToFile() {
fwrite(puffer, 1, m_p - puffer, stdout);
m_p = puffer;
}
~COutBuff() {
ToFile();
}
private:
inline void AuotToFile() {
if (m_p - puffer > N - 100) {
ToFile();
}
}
char puffer[N], * m_p;
};
template<int N = 1'000'000>
class CInBuff
{
public:
inline CInBuff() {}
inline CInBuff<N>& operator>>(char& ch) {
FileToBuf();
while (('\r' == *S) || ('\n' == *S) || (' ' == *S)) { S++; }//忽略空格和回车
ch = *S++;
return *this;
}
inline CInBuff<N>& operator>>(int& val) {
FileToBuf();
int x(0), f(0);
while (!isdigit(*S))
f |= (*S++ == '-');
while (isdigit(*S))
x = (x << 1) + (x << 3) + (*S++ ^ 48);
val = f ? -x : x; S++;//忽略空格换行
return *this;
}
inline CInBuff& operator>>(long long& val) {
FileToBuf();
long long x(0); int f(0);
while (!isdigit(*S))
f |= (*S++ == '-');
while (isdigit(*S))
x = (x << 1) + (x << 3) + (*S++ ^ 48);
val = f ? -x : x; S++;//忽略空格换行
return *this;
}
template<class T1, class T2>
inline CInBuff& operator>>(pair<T1, T2>& val) {
*this >> val.first >> val.second;
return *this;
}
template<class T1, class T2, class T3>
inline CInBuff& operator>>(tuple<T1, T2, T3>& val) {
*this >> get<0>(val) >> get<1>(val) >> get<2>(val);
return *this;
}
template<class T1, class T2, class T3, class T4>
inline CInBuff& operator>>(tuple<T1, T2, T3, T4>& val) {
*this >> get<0>(val) >> get<1>(val) >> get<2>(val) >> get<3>(val);
return *this;
}
template<class T = int>
inline CInBuff& operator>>(vector<T>& val) {
int n;
*this >> n;
val.resize(n);
for (int i = 0; i < n; i++) {
*this >> val[i];
}
return *this;
}
template<class T = int>
vector<T> Read(int n) {
vector<T> ret(n);
for (int i = 0; i < n; i++) {
*this >> ret[i];
}
return ret;
}
template<class T = int>
vector<T> Read() {
vector<T> ret;
*this >> ret;
return ret;
}
private:
inline void FileToBuf() {
const int canRead = m_iWritePos - (S - buffer);
if (canRead >= 100) { return; }
if (m_bFinish) { return; }
for (int i = 0; i < canRead; i++)
{
buffer[i] = S[i];//memcpy出错
}
m_iWritePos = canRead;
buffer[m_iWritePos] = 0;
S = buffer;
int readCnt = fread(buffer + m_iWritePos, 1, N - m_iWritePos, stdin);
if (readCnt <= 0) { m_bFinish = true; return; }
m_iWritePos += readCnt;
buffer[m_iWritePos] = 0;
S = buffer;
}
int m_iWritePos = 0; bool m_bFinish = false;
char buffer[N + 10], * S = buffer;
};
template<long long MOD = 1000000007, class T1 = int, class T2 = long long>
class C1097Int
{
public:
C1097Int(T1 iData = 0) :m_iData(iData% MOD)
{
}
C1097Int(T2 llData) :m_iData(llData% MOD) {
}
C1097Int operator+(const C1097Int& o)const
{
return C1097Int(((T2)m_iData + o.m_iData) % MOD);
}
C1097Int& operator+=(const C1097Int& o)
{
m_iData = ((T2)m_iData + o.m_iData) % MOD;
return *this;
}
C1097Int& operator-=(const C1097Int& o)
{
m_iData = ((T2)MOD + m_iData - o.m_iData) % MOD;
return *this;
}
C1097Int operator-(const C1097Int& o)const
{
return C1097Int(((T2)MOD + m_iData - o.m_iData) % MOD);
}
C1097Int operator*(const C1097Int& o)const
{
return((T2)m_iData * o.m_iData) % MOD;
}
C1097Int& operator*=(const C1097Int& o)
{
m_iData = ((T2)m_iData * o.m_iData) % MOD;
return *this;
}
C1097Int operator/(const C1097Int& o)const
{
return *this * o.PowNegative1();
}
C1097Int& operator/=(const C1097Int& o)
{
*this *= o.PowNegative1();
return *this;
}
bool operator==(const C1097Int& o)const
{
return m_iData == o.m_iData;
}
bool operator<(const C1097Int& o)const
{
return m_iData < o.m_iData;
}
C1097Int pow(T2 n)const
{
C1097Int iRet = (T1)1, iCur = *this;
while (n)
{
if (n & 1)
{
iRet *= iCur;
}
iCur *= iCur;
n >>= 1;
}
return iRet;
}
C1097Int PowNegative1()const
{
return pow(MOD - 2);
}
T1 ToInt()const
{
return ((T2)m_iData + MOD) % MOD;
}
private:
T1 m_iData = 0;;
};
typedef C1097Int< 998244353> BI;
class Solution {
public:
vector<int> Ans(long long n, const vector<pair<long long, long long>>& que) {
vector<int> ans;
for (const auto& [r, c] : que) {
auto GetZ = [&](long long r, long long c) {
if (c > 61) { return n; }
long long z = c;
const long long c2 = 1LL << c;
long long z2 = c2;
while ((z < n) && (0 == (r / c2 + r / z2) % 2) && (r >= z2)) {
z++; z2 *= 2;
}
if (z == n) { return z; }
if (0 != (r / c2 + r / z2)) { return z; }
if (r / c2 & 1) { return z; }
return n;
};
BI cur = BI(2).pow(GetZ(r, c)) - 1;
ans.emplace_back(cur.ToInt());
}
return ans;;
};
};
int main() {
#ifdef _DEBUG
freopen("a.in", "r", stdin);
#endif // DEBUG
ios::sync_with_stdio(0); cin.tie(nullptr);
//CInBuff<> in; COutBuff<10'000'000> ob;
long long N;
cin >> N;
auto que = Read<pair<long long, long long>>();
#ifdef _DEBUG
//printf("N=%d,M=%d", N,M);
//Out(v, ",v=");
//Out(B, ",B=");
//Out(que, ",que=");
#endif // DEBUG
Solution slu;
auto res = Solution().Ans(N,que);
for (const auto& i : res) {
cout << i << "\n";
}
return 0;
}
单元测试
TEST_METHOD(TestMethod1)
{
auto res = Solution().Ans(2, { {1,1},{2,0} });
AssertEx({3,1}, res);
}
扩展阅读
我想对大家说的话 |
---|
工作中遇到的问题,可以按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。 |
学习算法:按章节学习《喜缺全书算法册》,大量的题目和测试用例,打包下载。重视操作 |
有效学习:明确的目标 及时的反馈 拉伸区(难度合适) 专注 |
闻缺陷则喜(喜缺)是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。 |
子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。 |
如果程序是一条龙,那算法就是他的是睛 |
失败+反思=成功 成功+反思=成功 |
视频课程
先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。
https://edu.csdn.net/course/detail/38771
如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.csdn.net/lecturer/6176
测试环境
操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 C++17
如无特殊说明,本算法用**C++**实现。