Codeforces55D Beautiful numbers
2024-08-28 14:04:10
原题链接
虽然依旧是套模板,但是因为我太弱了,不会建状态,所以去看了题解。。
这里就直接引用我看的题解吧,写的不错的。
题解
//我的代码
#include<cstdio>
#include<cstring>
using namespace std;
const int mod = 2520;
const int N = mod + 10;
typedef long long ll;
int a[20], lc[N], l;
ll f[20][N][50];
inline ll re()
{
ll x = 0;
char c = getchar();
bool p = 0;
for (; c < '0' || c > '9'; c = getchar())
p |= c == '-';
for (; c >= '0' && c <= '9'; c = getchar())
x = x * 10 + c - '0';
return p ? -x : x;
}
int gcd(int x, int y)
{
if (!y)
return x;
return gcd(y, x % y);
}
int LCM(int x, int y)
{
if (!y)
return x;
return x / gcd(x, y) * y;
}
ll dfs(int pos, int nw, int lcm, int lm)
{
if (pos < 0)
return nw % lcm ? 0 : 1;
if (!lm && f[pos][nw][lc[lcm]] > -1)
return f[pos][nw][lc[lcm]];
int i, k = lm ? a[pos] : 9;
ll s = 0;
for (i = 0; i <= k; i++)
s += dfs(pos - 1, (nw * 10 + i) % mod, LCM(lcm, i), lm && i == a[pos]);
if (!lm)
return f[pos][nw][lc[lcm]] = s;
return s;
}
ll calc(ll x)
{
int k = 0;
do
{
a[k++] = x % 10;
x /= 10;
} while (x > 0);
return dfs(k - 1, 0, 1, 1);
}
int main()
{
int i, t;
ll n, m;
for (i = 1; i * i <= mod; i++)
if (!(mod % i))
{
lc[mod / i] = ++l;
lc[i] = ++l;
}
memset(f, -1, sizeof(f));
t = re();
for (i = 1; i <= t; i++)
{
n = re();
m = re();
printf("%I64d\n", calc(m) - calc(n - 1));
}
return 0;
}
最新文章
- swift 判断字符串长度
- Linux system函数详解
- [AHOI2013]找硬币(搜索)
- 每日一九度之 题目1030:毕业bg
- WebStorm 对 Mocha 完美支持。
- win7 开启休眠
- MySQL架构优化实战系列2:主从复制同步与查询性能调优
- Linux Bash终端支持中文显示
- Ubuntu_16.04_Lamp
- Linux中kettle启动spoon.sh遇到的问题
- Docker系列一之基础快速入门企业实战
- javascript中的时间版运动
- 开放windows服务器端口-----以打开端口8080为例
- GMM算法的matlab程序
- JQ面向对象的放大镜
- nvidia驱动自动更新版本后问题解决 -- failed to initialize nvml: driver/library version mismatch
- C语言数据结构基础学习笔记——静态查找表
- 性能监控扩展篇(grafana + influxdb + telegraf)
- EntityFramework 基础提供程序在 Open 上失败
- Linux中ls -l(ll)返回结果中的文件访问权限-rw-r--rw-