[思维]NIO‘s Sword 2022牛客多校第4场 K

82 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

题目描述

NIO is playing a new game. In this game, NIO has a sword with attack power represented by an integer AAA. Initially, A=0A=0A=0. There are also nnn enemies in the game. NIO has to kill them in order, which means NIO must kill the (i−1)(i-1)(i−1)-th enemy before killing the iii-th enemy for each iii (2≤i≤n2\le i\le n2≤i≤n). For the iii-th enemy, NIO can kill it if and only if A≡i(modn)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 xxx (0≤x≤90\le x\le 90≤x≤9), then changes the attack power of the sword from AAA to 10×A+x10\times A+x10×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 nnn (1≤n≤1061\le n\le 10^{6}1≤n≤106), indicating the number of enemies.

输出描述:

Output one integer indicating the minimum times to upgrade the sword.

示例1

输入

4

输出

4

题意:

有n个敌人,1把剑,开始时剑的攻击力为0,击败第i个敌人需要剑的攻击力与i关于n同余,同时你可以对剑进行升级,每次升级可以在攻击力数值后面增添一位,问击败所有敌人的最少升级次数,必须先击败第i个敌人才能击败第i+1个敌人。

分析:

可以发现每次击败敌人需要升级次数最多不会超过n的位数,这点通过鸽巢原理可知,那么就可以从1开始枚举需要添加几位数字,如果添加i位数字就能击败敌人,那么一定存在一个x使得y*now+x同余1,y是一个由i位9构成的常数并且x∈[0, y],其实也就是攻击力的增量和1同余的意思,于是就可以从1开始枚举要添加几位数字了,O(1)判断出是否能击败敌人,最终统计一下答案即可,最后要注意n = 1的情况答案是0而不是1。

具体代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#define int long long
using namespace std;

int ten[10] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000};

signed main()
{
	int n;
	cin >> n;
	if(n == 1) puts("0");
	else{
		int ans = 0;
		for(int i = 0; i < n; i++){
			int now = i;
			for(int j = 1; j <= 7; j++){//添加j位
				int l = (ten[j]-1)*now%n, r = l+ten[j]-1;
				if(!(l > 1 && r < n+1 || l == 0 && r == 0)){
					ans += j;
					break;
				}
			}
		}
		cout << ans << endl;
	}
    return 0;
}