牛客暑假四 K NIO‘s Sword 【多变量式子减变量】【数学推导】【同余推导】

链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
 

题目描述

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 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

输出

复制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=110\times0+1=110×0+1=1.
- The attack power of the sword when killing the second enemy can be 10×1+4=1410\times1+4=1410×1+4=14.
- The attack power of the sword when killing the third enemy can be 10×14+7=14710\times14+7=14710×14+7=147.
- The attack power of the sword when killing the fourth enemy can be 10×147+2=147210\times147+2=147210×147+2=1472.

So he needs to upgrade the sword at least four times.

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

bool check(LL c,int l)
{
    int co=0;
    while(c)
    {
        co++;
        c=c/10;
    }
    
    if(co<=l) return true;
    else return false;
}


int main()
{
    int n;
    cin>>n;
    
    if(n==1)  //当只有一个敌人的时候,最少升级次数是0需要特判!
    {
                     //1%1==0%1
        puts("0");
        return 0;
    }
    
    
    LL sum=0;
    for(int i=1;i<=n;i++)
    {
        for(int L=1;L<=6;L++)
        {
            LL C=((i+1-(LL)i*(int)pow(10,L))%n+n)%n;
            if(check(C,L)) //判断C的位数是否满足小于等于L位
            {
                sum+=L;
                break;
            }
        }
    }
    
    cout<<sum<<endl;
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值