logo AlgoBeat OnlineJudge
登录 注册

题解

作者: BannedUser767  ·  发布于 2026-09-02 19:16:25  ·  最后修改于 2026-09-03 21:25:23
已通过
审核员:AlgoBeat 官方账号 · 2026-09-03 21:25:23

题目大意

求 2239 年 9 月 9 日到 9876 年 1 月 1 日中午、月、日出现过的数码(不含前导 0)出现次数均相同的日期数量。

大致思路

模拟。

我们枚举对应时间段中的每一个日期,对枚举到的日期的年、月、日进行数位分离,并用一个桶存储对应数码出现的次数。处理完之后,我们遍历对应数组中的每个无序对,若组成该数对的两个元素均不为 0 且不相等,则该日期不合法,否则答案增加 1。

一个坑点是年份的上限不能直接设置为 9876 且不对月、日的上限特殊处理,因为这样可能会造成把 9876 年的所有日期全都遍历一遍,统计这部分答案。解决办法是将年的上限设为 9875,由于 9876 年 1 月 1 日不对答案作出贡献,这样做是可行的。作者因此痛失 50 pts。

最终代码

#include<bits/stdc++.h>
using namespace std;
const int day[13]={-1,31,28,31,30,31,30,31,31,30,31,30,31};
bool isrun(int y) {return y%400==0||y%4==0&&y%100!=0;}
int ans, cnt[15];
void cl(int x) {while(x) cnt[x%10]++,x/=10;}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    for(int y=2239;y<=9875;y++)
    {
    for(int m=(y-=2239?9:1);m<=12;m++)
    {
    int sx=(isrun(y)?(m-=2?29:day[m]):day[m]);
    for(int d=(y-=2239&m=9?9:1);d<=sx;d++)
    {
    memset(cnt,0,sizeof(cnt)),cl(y),cl(m),cl(d);
    bool can=true;
    for(int i=0;i<9;i++)
    for(int j=i+1;j<=9;j++)
    if(cnt[i]&&cnt[j]&&cnt[i]!=cnt[j]){can=false;break;}
    if(can) ans++;
    }
    }
    cout<<ans;
    return 0;
}

时间复杂度:

空间复杂度:

暂无评论

登录 后即可评论。