鱼C论坛

 找回密码
 立即注册
查看: 2502|回复: 2

题目170: 求最大的可以由乘积相连得到的0-9的pandigital数字

[复制链接]
发表于 2016-9-15 01:01:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Find the largest 0 to 9 pandigital that can be formed by concatenating products

Take the number 6 and multiply it by each of 1273 and 9854:

        6 × 1273 = 7638
        6 × 9854 = 59124

By concatenating these products we get the 1 to 9 pandigital 763859124. We will call 763859124 the "concatenated product of 6 and (1273,9854)". Notice too, that the concatenation of the input numbers, 612739854, is also 1 to 9 pandigital.

The same can be done for 0 to 9 pandigital numbers.

What is the largest 0 to 9 pandigital 10-digit concatenated product of an integer with two or more other integers, such that the concatenation of the input numbers is also a 0 to 9 pandigital 10-digit number?


题目:

用数字 6 分别乘以 1273 和 9854,得到:

          6 × 1273 = 7638
          6 × 9854 = 59124

将结果连结起来,我们可以得到 1-9 的 pandigital 数字,763859124。我们把 763859124 叫做“6 和 (1273,9854)的连结乘积”。同样要注意到,将输入数字相连接得到的 612739854,也是 1-9 的 pandigital 数字。

对 0-9 的 pandigital 数字,同样的规则也成立。

按照以上过程,将一个整数与两个或更多的其它整数分别相乘,将积相连后得到一个数字 N,N 是 10 位的 0-9 pandigital 数字,同时,输入的这几个数字本身相连,也必须是 10 位的 0-9 pandigital 数字。

现在请问,满足上述条件的 N 的最大值是多少?


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-9-21 14:46:07 | 显示全部楼层
本帖最后由 jerryxjr1220 于 2017-9-21 14:52 编辑

先倒序生成所有pandigital,然后分段,再依次判断是否符合要求,找到即终止程序。
  1. from itertools import permutations
  2. from math import gcd
  3. digitsR = [str(i) for i in range(10)][::-1]
  4. for p in permutations(digitsR):
  5.     for i in range(1,9):
  6.         p1 = int(''.join(p[:i]))
  7.         p2 = int(''.join(p[i:]))
  8.         g = gcd(p1,p2)
  9.         if g>1:
  10.             q1 = p1//g
  11.             q2 = p2//g
  12.             if sorted([d for m in [g,q1,q2] for d in str(m)], reverse=True) == digitsR:
  13.                 print (g,q1,q2,''.join(p))
  14.                 exit()
复制代码

27 36508 149 9857164023
[Finished in 0.4s]
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-13 21:06:11 | 显示全部楼层
本帖最后由 guosl 于 2022-10-14 21:35 编辑

思路同上,但上面的代码只把原数分成了两个部分,我们现在用递归进行完全分解。
答案:9857164023(耗时不到1秒)
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <omp.h>
  7. using namespace std;

  8. char a[12] = "9876543210";
  9. bool bContinue = true;

  10. long long GCD(long long a, long long b) //求a,b的最大公因数
  11. {
  12.   if (b == 0)
  13.     return a;
  14.   long long t = a % b;
  15.   while (t != 0)
  16.   {
  17.     a = b;
  18.     b = t;
  19.     t = a % b;
  20.   }
  21.   return b;
  22. }

  23. void fillbits(unsigned char *bs, long long k)
  24. {
  25.   while (k > 0)
  26.   {
  27.     ++bs[k % 10];
  28.     k /= 10;
  29.   }
  30. }

  31. bool chk(vector<long long> v)
  32. {
  33.   if (v.size() == 1)
  34.     return false;
  35.   long long nUp;
  36.   if (v.size() == 2)
  37.     nUp = 99;
  38.   else if (v.size() > 2)
  39.     nUp = 9;
  40.   long long d = v[0];
  41.   for (int i = 1; i < (int)v.size(); ++i)
  42.     d = GCD(d, v[i]);
  43.   d = min(d, nUp);
  44.   for (int i = 2; i <= d; ++i)
  45.   {
  46.     if ((d % i) == 0)
  47.     {
  48.       unsigned char bs[10];
  49.       memset(bs, 0, sizeof(bs));
  50.       fillbits(bs, i);
  51.       for (int j = 0; j < (int)v.size(); ++j)
  52.         fillbits(bs, v[j] / i);
  53.       bool bSuccess = true;
  54.       for (int j = 0; j < 10; ++j)
  55.       {
  56.         if (bs[j] != 1)
  57.         {
  58.           bSuccess = false;
  59.           break;
  60.         }
  61.       }
  62.       if (bSuccess)
  63.         return true;
  64.     }
  65.   }
  66.   return false;
  67. }

  68. bool dfs(vector<long long> v, int k, char *b)
  69. {
  70.   long long x = 0;
  71.   x = atoll(b + k);
  72.   vector<long long> v1 = v;
  73.   v1.push_back(x);
  74.   if (chk(v1))
  75.     return true;
  76.   v1.pop_back();
  77.   x = 0;
  78.   for (int i = k; i < 9; ++i)
  79.   {
  80.     x = 10 * x + int(b[i] - '0');
  81.     v1.push_back(x);
  82.     if (dfs(v1, i + 1, b))
  83.       return true;
  84.     v1.pop_back();
  85.   }
  86.   return false;
  87. }

  88. int main(void)
  89. {
  90.   long long nMax = 0;
  91. #pragma omp parallel shared(a,bContinue) reduction(max:nMax)
  92.   do
  93.   {
  94.     char a1[12];
  95. #pragma omp critical
  96.     {
  97.       memcpy(a1, a, sizeof(a));
  98.       bContinue = prev_permutation(a, a + 10);
  99.     }
  100.     long long x = 0;
  101.     for (int i = 0; i < 9; ++i)
  102.     {
  103.       vector<long long> v;
  104.       x = 10 * x + int(a1[i] - '0');
  105.       v.push_back(x);
  106.       if (dfs(v, i + 1, a1))
  107.       {
  108.         nMax = max(nMax, atoll(a1));
  109. #pragma omp critical
  110.         bContinue = false;
  111.         break;
  112.       }
  113.     }
  114.   }
  115.   while (bContinue);
  116.   cout << nMax << endl;
  117.   return 0;
  118. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-20 01:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表