链接: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
(i−1)−th enemy before killing the
i
−
t
h
i-th
i−th enemy for each
i
(
2
≤
i
≤
n
)
i (2\le i\le n)
i(2≤i≤n). For the ii-th enemy, NIO can kill it if and only if
A
≡
i
(
m
o
d
n
)
A\equiv i \pmod{n}
A≡i(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(0≤x≤9), 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(1≤n≤106)
), 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} A≡i(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−的原因。