Magic Multiple

本文介绍了一个算法,用于确定在锻造过程中,为了使剑的威力达到最大,所需的金属厚度(k)至少应为多少。算法通过检查从n倍到kn倍的数字序列中是否包含0到9的所有数字来实现这一目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击打开链接

 

 

Problem F: Magic Multiple

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 36   Solved: 23
[ Submit][ Status][ Web Board]

Description

The Elvish races of Middle Earth believed that certain numbers were more significant than
others. When using a particular quantity n of metal to forge a particular sword, they believed that
sword would be most powerful if the thickness k were chosen according to the following rule:
Given a nonnegative integer n, what is the smallest k such that the decimal representations of
the integers in the sequence:
n,2n,3n,4n,5n,...,kn
contain all ten digits (0 through 9) at least once?
Lord Elrond of Rivendell has commissioned you with the task to develop an algorithm to find
the optimal thickness (k) for any given quantity of metal (n).

Input

Input will consist of a single integer n per line. The end of input will be signaled by end of file.
The input integer will be between 1 and 200,000,000, inclusive.

Output

The output will consist of a single integer per line, indicating the value of k needed such that every
digit from 0 through 9 is seen at least once.

Sample Input

1
10
123456789
3141592

Sample Output

10
9
3
5

 

题意:输入n,让你求最小的k。要求是1*n,2*n.......k*n包含0到 9所有的数字。

#include<stdio.h>
#include<string.h>
int s[10];
int main()
{
    int i,j;
    long long n;
    while(scanf("%lld",&n)!=EOF)
    {
       long long  flag;
       int flag1,x;
        memset(s,0,sizeof(s));
        for(i=1;;i++)
        {
            flag=n*i;
            flag1=0;
            while(flag)
            {
                x=flag%10;
                s[x]=1;
                flag/=10;
            }
            for(j=0; j<=9; j++)
            {
                if(s[j]==0)
                {
                    flag1=1;
                    break;
                }
            }
            if(!flag1)
            {
                printf("%d\n",i);
                break;
            }
        }
    }
    return 0;
}


 

 

 

 

这段代码实现了寻找第 `n` 个神奇数字的问题。所谓“神奇数字”是指可以被两个正整数 `a` `b` 整除的最小公倍数序列中的元素。 ### 解释 #### 主函数 `nthMagicalNumber` ```java public static int nthMagicalNumber(int n, int a, int b) ``` 该方法用于计算第 `n` 个魔法数字,并返回结果对 \(10^9 + 7\) 取模后的值: - **变量初始化**: - 定义了常量 `MOD = 1_000_000_007`,表示取模操作所需的数值。 - 计算搜索范围 `[low, high]` 的初始边界。这里假设魔法数字不会超过较小的那个因子以 `n` 次方。 - **二分查找法**: 使用二分查找算法缩小可能的答案区间,直到找到满足条件的第一个魔力数字为止。 ```java while(low < high){ long mid = low + (high - low)/2; if(isNthMagicNumber(mid,n,a,b)){ high = mid; }else{ low = mid+1; } } ``` - 返回最终确定的结果并对它余运算得到输出形式: ```java return (int)(low % MOD); ``` #### 辅助判断函数 `isNthMagicNumber` ```java private static boolean isNthMagicNumber(long num,long target,int A,int B) ``` 此辅助功能检查给定范围内是否存在足够多的小于等于某特定值且能被A或B任意一方整除的数量是否不少于目标数目: - 首先通过调用LCM()获得两者的最大公约数; - 然后统计小于当前猜测点mid并同时能够被这两个参数之一整除的所有自然数; - 最终比较计数值是否达到预期的目标数目target. #### 公约数公倍数的相关工具函数 ```java private static double LCM(int A, int B) private static int gcd(int p, int q) ``` * 第一个是用来获取两者之间最小公倍数(LCM)的一个静态成员方法; * 而第二个则是经典的递归版本的最大公约数(GCD),它是基于欧几里得原理实现的一种高效算法,在很多数学应用领域都有广泛的应用. ### 总结说明: 整个程序的核心思想就是利用二叉树形结构快速定位到符合条件的位置上,结合简单的数论知识(如最大公约数、最小公倍数等),从而有效地解决了问题。这种方法不仅提高了效率而且保证了一定程度上的准确率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值