鱼C论坛

 找回密码
 立即注册
查看: 2631|回复: 1

题目259:可达数

[复制链接]
发表于 2017-1-13 15:42:34 | 显示全部楼层 |阅读模式

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

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

x
Reachable Numbers

A positive integer will be called reachable if it can result from an arithmetic expression obeying the following rules:

  • Uses the digits 1 through 9, in that order and exactly once each.
  • Any successive digits can be concatenated (for example, using the digits 2, 3 and 4 we obtain the number 234).
  • Only the four usual binary arithmetic operations (addition, subtraction, multiplication and division) are allowed.
  • Each operation can be used any number of times, or not at all.
  • Unary minus is not allowed.
  • Any number of (possibly nested) parentheses may be used to define the order of operations.
  • For example, 42 is reachable, since (1/23) * ((4*5)-6) * (78-9) = 42.


What is the sum of all positive reachable integers?


题目:

一个正整数如果由满足以下条件的算术表达式得到则称之为可达的。

  • 使用数字 1 到 9,按照顺序并且每个数字用且只用一次。
  • 任何连续的数字可以串联(例如,使用数字 2,3,4 我们将得到数字 234)。
  • 只允许有 4 种基本二元算术运算(加法,减法,乘法,除法)。
  • 任何一种运算可以使用任意次,或者不使用。
  • 不允许一元减法。(也就是负号)
  • 为了定义运算顺,可以使用任意个括号。
  • 例如,42 是可达的,因为 (1/23) * ((4*5)-6) * (78-9) = 42。


所有可达正整数的和为多少?


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-6-10 13:51:05 | 显示全部楼层
本帖最后由 天之南 于 2017-6-12 17:12 编辑
  1. import java.util.HashSet;

  2. public class ProjectEuler_259{
  3.         static final int LEN = 9;
  4.         static Fraction[][][] arr = new Fraction[LEN + 1][LEN + 1][];
  5.         static HashSet<Fraction> tmp = new HashSet<>();
  6.         static {
  7.                 for (int i = 0; i <= LEN; i++) {
  8.                         arr[i][i] = new Fraction[] { new Fraction(i) };
  9.                 }
  10.         }

  11.         public static void main(String[] args) {

  12.                 for (int len = 2; len <= LEN; len++) {
  13.                         for (int i = 1, j = i + len - 1; j <= LEN; i++, j++) {
  14.                                 tmp.clear();
  15.                                 for (int k = i; k < j; k++) {
  16.                                         if (len < 9)
  17.                                                 tmp.addAll(cartessian(arr[i][k], arr[k + 1][j]));
  18.                                         else
  19.                                                 tmp.addAll(cartessianFix(arr[i][k], arr[k + 1][j]));
  20.                                 }
  21.                                 tmp.add(new Fraction(joint(i, j)));
  22.                                 arr[i][j] = new Fraction[tmp.size()];
  23.                                 tmp.toArray(arr[i][j]);
  24.                         }
  25.                 }
  26.                
  27.                 long sum = 0;
  28.                 for (Fraction x : arr[1][LEN]) {
  29.                         sum += x.toLong();
  30.                 }
  31.                 System.out.println(sum);

  32.         }

  33.         static HashSet<Fraction> cartessian(Fraction[] a, Fraction[] b) {
  34.                 HashSet<Fraction> tmp = new HashSet<>();
  35.                 for (Fraction x : a) {
  36.                         for (Fraction y : b) {
  37.                                 tmp.add(x.add(y));
  38.                                 tmp.add(x.mul(y));
  39.                                 tmp.add(x.sub(y));
  40.                                 if (y.n != 0)
  41.                                         tmp.add(x.div(y));
  42.                         }
  43.                 }
  44.                 return tmp;
  45.         }

  46.         static HashSet<Fraction> cartessianFix(Fraction[] a, Fraction[] b) {
  47.                 HashSet<Fraction> tmp = new HashSet<>();
  48.                 for (Fraction x : a) {
  49.                         for (Fraction y : b) {
  50.                                 addFix(tmp, x.add(y));
  51.                                 addFix(tmp, x.mul(y));
  52.                                 addFix(tmp, x.sub(y));
  53.                                 if (y.n != 0)
  54.                                         addFix(tmp, x.div(y));
  55.                         }
  56.                 }
  57.                 return tmp;
  58.         }

  59.         static int joint(int i, int j) {
  60.                 int res = 0;
  61.                 for (int x = j, pow = 1; x >= i; x--, pow *= 10) {
  62.                         res = res + pow * x;
  63.                 }
  64.                 return res;
  65.         }

  66.         static void addFix(HashSet<Fraction> set, Fraction f) {
  67.                 if (f.n > 0 && f.d == 1) {
  68.                         set.add(f);
  69.                 }
  70.         }

  71. }

  72. //分数类
  73. class Fraction {
  74.         public int n = 0;//分子numerator
  75.         public int d = 1;//分母denominator

  76.         public Fraction() {
  77.                
  78.         }

  79.         public Fraction(int z) {
  80.                 this.n = z;
  81.                 this.d = 1;
  82.         }

  83.         // addition, subtraction, multiplication, division:加减乘除运算
  84.         public Fraction add(Fraction x) {
  85.                 Fraction res = new Fraction();
  86.                 res.n = this.n * x.d + this.d * x.n;
  87.                 res.d = this.d * x.d;
  88.                 res.simple();
  89.                 return res;
  90.         }

  91.         public Fraction sub(Fraction x) {
  92.                 Fraction res = new Fraction();
  93.                 res.n = this.n * x.d - this.d * x.n;
  94.                 res.d = this.d * x.d;
  95.                 res.simple();
  96.                 return res;
  97.         }

  98.         public Fraction mul(Fraction x) {
  99.                 Fraction res = new Fraction();
  100.                 res.n = this.n * x.n;
  101.                 res.d = this.d * x.d;
  102.                 res.simple();
  103.                 return res;
  104.         }

  105.         public Fraction div(Fraction x) {
  106.                 Fraction res = new Fraction();
  107.                 res.n = this.n * x.d;
  108.                 res.d = this.d * x.n;
  109.                 res.simple();
  110.                 return res;
  111.         }
  112.        
  113.         //化简
  114.         public void simple() {
  115.                 if (n % d == 0) {
  116.                         n = n / d;
  117.                         d = 1;
  118.                         return;
  119.                 }
  120.                 int min = Math.min(n, d);
  121.                 for (int i = 2; i*i <= min; i++) {
  122.                         while (n % i == 0 && d % i == 0) {
  123.                                 n /= i;
  124.                                 d /= i;
  125.                         }
  126.                 }
  127.                 if (d < 0) {
  128.                         n = -n;
  129.                         d = -d;
  130.                 }
  131.         }

  132.         public long toLong() {
  133.                 return n / d;
  134.         }

  135.         @Override
  136.         public int hashCode() {
  137.                 final int prime = 31;
  138.                 int result = 1;
  139.                 result = prime * result + d;
  140.                 result = prime * result + n;
  141.                 return result;
  142.         }

  143.         @Override
  144.         public boolean equals(Object obj) {
  145.                 if (this == obj)
  146.                         return true;
  147.                 if (obj == null)
  148.                         return false;
  149.                 if (getClass() != obj.getClass())
  150.                         return false;
  151.                 Fraction other = (Fraction) obj;
  152.                 if (d != other.d)
  153.                         return false;
  154.                 if (n != other.n)
  155.                         return false;
  156.                 return true;
  157.         }

  158.         @Override
  159.         public String toString() {
  160.                 if (d == 1)
  161.                         return "" + n;
  162.                 return n + "/" + d;
  163.         }
  164. }
复制代码


答案:20101196798
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-25 08:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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