CF1992E Novice's Mistake
同步于个人博客。
Problem
Noobish_Monk 有 \(n\in [1,100]\) 个朋友。每个朋友都给了他 \(a\in [1,10000]\) 个苹果作为生日礼物。Noobish_Monk收到礼物后非常高兴,他把 \(b\in[1,\min\{10000,a\cdot n\}]\) 个苹果还给了朋友们。Noobish_Monk 还剩下多少个苹果?"
K1o0n 写了一个解法,但不小心把 \(n\) 的值看成了字符串,所以 \(n \cdot a - b\) 的值的计算方法不同。具体来说
- 当用字符串 \(n\) 乘以整数 \(a\) 时,他将得到字符串 \(s=\underbrace{n + n + \dots + n + n}_{a\ \text{times}}\)。
- 从字符串 \(s\) 中减去整数 \(b\) 时,将删除最后的 \(b\) 个字符。如果 \(b\) 大于或等于字符串 \(s\) 的长度,则字符串将变为空。
现在 ErnKor 想知道在给定的 \(n\) 中,有多少对 \((a, b)\) 满足问题的约束条件且 K1o0n 的解法给出了正确答案。
“解法给出了正确答案”意味着得到了一个非空字符串,且这个字符串转换成整数后等于正确答案,即 \(n \cdot a - b\) 的值。
\(1\le t\le 100,1\le n \le 100\)
Solution
一个粗浅的办法就是,枚举\(a\in[1,1000],b\in[1,\min\{10000,a\cdot n\}]\),使用等比数列求和公式推式子,注意需要对于\(n\)的位数和\(b\)的值分类讨论。
但是由于 \(1\le t\le 100\) ,这个算法是过不去的。我发现答案是非常稀疏的,本来还想试试打表,但是被别人提醒了正解的方向,所以这个想法就搁置下来了(没必要了)。扔一份草稿在附录吧。
正解
我们规定,\(n * a-b\) 是指将 \(n\) 当作字符串重复 \(a\) 次再删去后 \(b\) 位,而 \(n\cdot a-b\) 是指数学意义上的乘法。\(|n|\) 指的是 \(n\) 的位数。
注意到 \(na-b\le 10^6\) ,这意味着 \(n*a-b\) 不能超过 \(6\) 位,也就是 \(1\le |n*a-b|\le6\) ,所以\(|n|\cdot a-6\le b\le |n|\cdot a-1\)。这样我们可以缩小 \(b\) 的枚举范围,只需要枚举 \(6a\) 次即可。
对于每一组 \((a,b)\) ,我们只需生成 \(n*a\) 的前 \(|n|\cdot a-b\) 位,判断其是否与 \(na-b\) 相等,即可做到快速判断 \(n*a-b=na-b\) 是否成立。
Code
vector<pair<int,int>>ans;
int main()
{
int t=1;
cin>>t;
while(t--)
{
LL n;
ans.clear();
cin>>n;
string nn=to_string(n);
for(LL a=1;a<=10000;a++)
{
for(LL b=max(1ll,nn.size()*a-6);b<=nn.size()*a-1;b++)
{
string x;
int wei=nn.size()*a-b;
for(int i=0;i<wei;i++)
{
x.push_back(nn[i%nn.size()]);
}
if(x==to_string(n*a-b)) ans.push_back(make_pair(a,b));
}
}
cout<<ans.size()<<endl;
for(unsigned int i=0;i<ans.size();i++){
cout<<ans[i].first<<" "<<ans[i].second<<endl;
}
}
return 0;
}
Appendix
\(|n|=1\)
n * a-b=\frac{n\cdot10^{a-b}-n}9
\end{gather}
\]
\(|n|=2\)
- 若 \(b\) 为偶数:
n * a-b=\frac{n\cdot100^{a-\frac{b}{2}}-1}{99}
\end{gather}
\]
- 若 \(b\) 为奇数:
n * a-b=\frac{n\cdot100^{a-\frac{b+1}{2}}-1}{99}\times 10+[n的十位]
\end{gather}
\]
\(|n|=3\) 即 \(n=100\)
- 若 \(b\equiv 0 \pmod{3}\)
n * a-b=\frac{100\times1000^{a-\frac{b}{3}}-1}{999}
\end{gather}
\]
- \(b\equiv 1 \pmod{3}\)
n * a-b=\frac{100\times1000^{a-\frac{b+2}{3}}-1}{999}\times 100 + 10
\end{gather}
\]
- \(b\equiv 2 \pmod{3}\)
n * a-b=\frac{100\times1000^{a-\frac{b+1}{3}}-1}{999}\times 10+1
\end{gather}
\]
CF1992E Novice's Mistake的更多相关文章
- make no mistake, we are the last line of defense.
make no mistake, we are the last line of defense.
- 动态规划(DP计数):HDU 5121 Just A Mistake
As we all know, Matt is an outstanding contestant in ACM-ICPC. Graph problems are his favorite.Once, ...
- [转] Are You Making a Big Mistake in This Volatile Market?
Stock market volatility continues unabated. It may be too early to tell, but I’m marking the top of ...
- beginner’s mistake
PHP Advanced and Object-Oriented Programming 3rd Edition Related to modularity is abstraction: class ...
- common mistake of closure in loops
[common mistake of closure in loops] 下例中item引用的始终是最后一个值. function showHelp(help) { document.getEleme ...
- Non-Nullable Types vs C#: Fixing the Billion Dollar Mistake (转载)
One of the top suggestions (currently #15 on uservoice) for improving C# is the addition of non-null ...
- Spoken English Practice( Nobody have the guts to tell Paul what a mistake he was taking.(call,come,gut,fortune,when it comes to))
音标复习 绿色:连读: 红色:略读: 蓝色:浊化: 橙色:弱读 口语蜕变(2017/6/24) If your ...
- Null is your firend, not a mistake
原文作者: Roman Elizarov 原文地址: Null is your firend, not a mistake 译者:秉心说 Kotlin Island from Wikimedia by ...
- HDU 5121 Just A Mistake
Just A Mistake Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) ...
- 【pwnable.kr】 mistake
又一道pwnable,我还没放弃.. ssh mistake@pwnable.kr -p2222 (pw:guest) 源代码如下: #include <stdio.h> #include ...
随机推荐
- 腾讯元宝登顶App Store免费榜榜首!国产AI APP混战升级
最近,国产AI APP市场热闹非凡,竞争愈发激烈.其中,腾讯元宝的表现格外亮眼,它在短短一周内,借助微信的强大助力,一路逆袭,成功登上苹果App Store免费榜榜首.这一变化不仅展现了腾讯在AI领域 ...
- mac brew 安装
Homebrew国内源 知乎文章地址:https://zhuanlan.zhihu.com/p/111014448 苹果电脑安装脚本: /bin/zsh -c "$(curl -fsSL h ...
- Go map字典排序
前言 我们已经知道 Go 语言的字典是一个无序集合,如果你想要对字典进行排序,可以通过分别为字典的键和值创建切片,然后通过对切片进行排序来实现. 按照键进行排序 如果要对字典按照键进行排序,可以这么做 ...
- go 定时任务库 cron
简介 在Linux中,Cron是计划任务管理系统,通过crontab命令使任务在约定的时间执行已经计划好的工作,例如定时备份系统数据.周期性清理缓存.定时重启服务等. 本文介绍的cron库是一个用于管 ...
- linux部署go项目
直接部署: 1.将程序所需要的文件如配置文件和生成的可执行文件拷贝到linux中 2.直接执行./main命令,启动程序 (main是go编译生成的可执行文件) 如果报Permission denie ...
- SpringBoot+微信支付-JSAPI
引入微信支付SDK Maven: com.github.wechatpay-apiv3:wechatpay-java-core:0.2.12 Maven: com.github.wechatpay-a ...
- Linux 离线升级 RSYNC
前言:本文操作是在 CentOS-7 下执行的,不确定在其他 Linux 发布版是否能同样正常执行. 1.检查前置依赖组件 在安装 rsync 之前,需要确认已安装了相关依赖组件: gcc .open ...
- SQLite 下载与安装
安装 SQLite 官网 就2个文件,下载解压到一起即可 这里不是安装包的形式,下载后是下面这2个文件: 解压到同一目录下即可,如图: "安装"完成后,记得添加系统环境变量,以便命 ...
- java学习-6-核心类:字符串StringJoiner 和数组一起玩
public class Main { public static void main(String[] args) { String[] names = {"Bob", &quo ...
- FDConnection lost后的处理right here