zoukankan      html  css  js  c++  java
  • BZOJ1263 [SCOI2006]整数划分

    Description

    从文件中读入一个正整数n(10≤n≤31000)。要求将n写成若干个正整数之和,并且使这些正整数的乘积最大。 例如,n=13,则当n表示为4+3+3+3(或2+2+3+3+3)时,乘积=108为最大。

    Input

    只有一个正整数: n (10≤n≤31000)

    Output

    第1行输出一个整数,为最大乘积的位数。 第2行输出最大乘积的前100位,如果不足100位,则按实际位数输出最大乘积。 (提示:在给定的范围内,最大乘积的位数不超过5000位)。

    Sample Input

    13

    Sample Output

    3
    108

    题解

    由于对于$a > 1, b > 1$, 有$ab geq a+b$(因为$ab-a-b=ab-a-b+1-1=(a-1)(b-1)-1geq 1-1=0$),所以在答案中一定只存在$2,3$两个数(因为其它数都可以写成它们的和)。

    那么,若$n>7$,那么答案中要么有三个以上的2,要么有两个以上的3,而$3^2=9>8=2^3$,所以答案一定有两个3.

    那么ans*=9;n-=6;直到$nleq 7$,这个时候直接查表。

    附代码:

    #include <cstdio>
    int len, ans[5050];
    void mul(int x) {
      int i, c;
      for (i = 0, c = 0; i < len || c; ++i) {
        c = (ans[i] = ans[i] * x + c) / 10;
        ans[i] %= 10;
      }
      len = i;
    }
    const int e[] = {0, 1, 2, 3, 4, 6, 9, 12};
    int main() {
      int n;
      scanf("%d", &n);
      len = 1, ans[0] = 1;
      while (n > 7) {
        mul(9);
        n -= 6;
      }
      mul(e[n]);
      printf("%d
    ", len);
      for (int i = len - 1; ~i && i >= len - 100; --i)
        printf("%d", ans[i]);
      return 0;
    }
    

      

  • 相关阅读:
    【LeetCode】Rotate Image
    【LeetCode】Combinations
    【LeetCode】Minimum Depth of Binary Tree
    【LeetCode】Reverse Nodes in k-Group
    【LeetCode】Reverse Linked List II
    【LeetCode】Insert Interval
    【LeetCode】Insertion Sort List
    python之列表生成式
    python 模块和模块sys.argv
    python 异常处理
  • 原文地址:https://www.cnblogs.com/y-clever/p/7028887.html
Copyright © 2011-2022 走看看