NIO‘s Sword题解报告

本文介绍了一款NIO参与的游戏,游戏中他需要杀死n个敌人,每次杀敌时剑的攻击力量A需满足A ≡ i (mod n)。NIO可以升级剑的攻击力量,寻找最小升级次数以完成游戏。通过分析输入输出示例,得出解决问题的关键在于数学计算和优化升级路径。代码展示了一个可能的解决方案,帮助理解问题本质。
摘要由CSDN通过智能技术生成

链接:https://ac.nowcoder.com/acm/contest/33189/K
来源:牛客网
题目描述
See Problem N for PDF statements.
NIO is playing a new game. In this game, NIO has a sword with attack power represented by an integer A A A. Initially, A = 0 A=0 A=0. There are also nn enemies in the game. NIO has to kill them in order, which means NIO must kill the ( i − 1 ) − t h (i−1)-th (i1)th enemy before killing the i − t h i-th ith enemy for each i ( 2 ≤ i ≤ n ) i (2\le i\le n) i(2in). For the ii-th enemy, NIO can kill it if and only if
A ≡ i ( m o d n ) A\equiv i \pmod{n} Ai(modn).

Fortunately, NIO can upgrade his sword at any moment for any number of times. Each time NIO upgrades his sword, he first chooses an integer x ( 0 ≤ x ≤ 9 ) x (0\le x\le 9) x(0x9), then changes the attack power of the sword from A t o 10 × A + x A to 10\times A+x Ato10×A+x.

NIO wants to minimize the number of times to upgrade the sword so that he can win the game as fast as possible. Can you help him to find the minimum times?
输入描述:
The first line contains one integer n n ( 1 ≤ n ≤ 1 0 6 ) nn (1\le n\le 10^{6}) nn(1n106)
), indicating the number of enemies.
输出描述:
Output one integer indicating the minimum times to upgrade the sword.
示例
输入

4

输出

4

说明
Here is a possible solution for the example:

  • The attack power of the sword when killing the first enemy can be 10 × 0 + 1 = 1. 10\times0+1=1. 10×0+1=1.
  • The attack power of the sword when killing the second enemy can be 10 × 1 + 4 = 14. 10\times1+4=14. 10×1+4=14.
  • The attack power of the sword when killing the third enemy can be 10 × 14 + 7 = 147. 10\times14+7=147. 10×14+7=147.
  • The attack power of the sword when killing the fourth enemy can be 10 × 147 + 2 = 1472. 10\times147+2=1472. 10×147+2=1472.
    So he needs to upgrade the sword at least four times.
    题目大意
    在这里插入图片描述
    具体分析
    本憨先将 A ≡ i ( m o d n ) A\equiv i \pmod{n} Ai(modn).翻译成本憨能看懂的东西——即 A m o d n = = i m o d n A mod n==imod n Amodn==imodn然后,这题该怎么做呢?
    其实,本题归根结底就是一道数学题。
    先献上代码,然后在代码中分析吧~~(因为直接分析有点累,个人感觉看代码理解更简单)~~
    献上代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll n,ans;//n表示有n个敌人,ans表示需要升级几次 
void find(ll b){
	ll a=b-1,k=0,l=0;
	b%=n;//本处很有必要,因为当b==n时,它必须是0 
	while(1){
		l=a%n;//让a和n求模,在此,我似乎也只能(很欠地)说是用来检查的各位巨佬自己理解下吧。。。 
		if(l>b&&b+n-l<=k)return;//这两个语句剪枝用,否则时间复杂度太大,会TLE 
		if(l<=b&&b-l<=k)return;
		a*=10;// 
		k=k*10+9;
		ans++;//表示这把剑升了一级 
	}
}
int main(){
    cin>>n;
    for(ll i=1;i<=n;i++) find(i);//本处枚举i,表示正在打第i个怪物 
    cout<<ans;//最后输出ans即可 
}

其实,发现代码也并不长,只要仔细,认真分析就不难得出答案。
B a C l 2 + 2 A g N O 3 = = B a ( N O 3 ) 2 + 2 A g C l ↓ BaCl_2+2AgNO_3==Ba(NO_3)_2+2AgCl\downarrow BaCl2+2AgNO3==Ba(NO3)2+2AgCl这就是为什么最好不要用 B a C l 2 BaCl_2 BaCl2检验 S O 4 2 − SO_4^{2-} SO42的原因。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值