跳到正文

题解

P2655 2038年问题 题解

bcdmwSjy约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟
获赞
4
题目
P2655

版本

  1. v1当前

显然,题目要我们求的是当前时间向后 2bit−1−12^{bit-1}-1 秒后的时间,而这个东西很容易用 ctime 中有关时间的函数求出。

我们只需要把秒数加上去然后剩下的交给 c++ 来完成就好了。

注意把秒直接加有可能爆 int,解决方法也很简单,把秒加上模 6060 的值,然后把分加上除以 6060 的值就行了。

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

void print(tm now){
	cout<<now.tm_year+1900<<" "<<now.tm_mon+1<<" "<<now.tm_mday<<" "<<now.tm_hour<<" "<<now.tm_min<<" "<<now.tm_sec<<"\n";
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int T;
	cin>>T;
	while (T--){
		ll bit;
		tm curr;
		cin>>bit>>curr.tm_year>>curr.tm_mon>>curr.tm_mday>>curr.tm_hour>>curr.tm_min>>curr.tm_sec;
		curr.tm_year-=1900; //tm 中年份表示为从 1900 年至今的年数
		curr.tm_mon--; //tm 中月份表示为 0~11
		ll sec=(1ll<<(bit-1))-1;
		curr.tm_sec+=sec%60; //直接加会爆int
		curr.tm_min+=sec/60;
		time_t t=mktime(&curr);
		print(*localtime(&t));
	}
	return 0;
}

附赠 python 代码:

Python
from datetime import *
for _ in range(int(input())):
    bit,a,b,c,d,e,f=map(int,input().split())
    now=datetime(a,b,c,d,e,f)+timedelta(seconds=2**(bit-1)-1)
    print(now.strftime("%Y %-m %-d %-H %-M %-S"))
原文