其他分享
首页 > 其他分享> > [CF997B] Roman Digits - 组合,构造

[CF997B] Roman Digits - 组合,构造

作者:互联网

[CF997B] Roman Digits - 组合,构造

Description

我们在研究罗马数字。罗马数字只有4个字符,I,V,X,L分别代表1,5,10,50 N 的罗马数字有多少种不同的值。\(n \le 10^9\)

Solution

首先我们可以把 1,5,10,50 换成 0,4,9,49

为了不重不漏,我们钦定,4 最多选 8 个,0 和 49 随便选

那么 9 应该选多少个?

\(49 = 5\times 9 + 1\times 4 = 9 \times 9 - 2 \times 4\)

所以在不选 4 的时候,9 最多选 8 个(否则就可以换成另一种合法情况)

在选 4 的时候,9 最多选 4 个(否则就可以换成另一种合法情况)

因此,对于一个 n,我们暴力枚举选了 i 个 4,选了 j 个 9,那么此时 0 和 49 随便选,方案数贡献 n-i-j+1

#include <bits/stdc++.h>
using namespace std;

#define int long long 

signed main()
{
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    int ans=0;
    for(int i=0;i<=8;i++)
    {
        for(int j=0;j<=(i==0?8:4);j++)
        {
            ans+=max(0ll,n-i-j+1);
        }
    }
    cout<<ans<<endl;
}

标签:Digits,10,最多选,49,int,CF997B,罗马数字,times,Roman
来源: https://www.cnblogs.com/mollnn/p/14484723.html