Timus 1727. znaika's magic numbers

Source: Internet
Author: User

Timus 1727. znaika's magic numbers requires that the specified positive integer be decomposed into the sum of some different positive integers.

1727. znaika's magic numberstime limit: 0.5 second
Memory limit: 64 mbznaika has interests. For example, now he is investing the properties of number sets. znaika writes down some set consisting of different positive integers (he cils this set GeneratingSet), calculates the sum of all the written digits, and writes down the result in a special notebook. for example, for a generating set 7, 12, 43, he will write down the number17 = 7 + 1 + 2 + 4 + 3. znaika is sure that only MagicNumbers can appear as a result of this operation. neznaika laughs at znaika. he thinks that there is a generating set for every number, and he even made a bet with znaika that he wocould be able to construct such a set. help neznaika win the bet and construct a generating set for a given number. inputthe only input line contains an integer N(0 < N<105). outputif it is possible to construct a generating set for the number N, Output the number of elements in this set in the first line. in the second line output a space-separated list of these elements. the elements of the set must be different positive integers Strictly less than 105. if there are several generating sets, output any of them. if there are no generating sets, output −1. sample
Input Output
17
37 12 43
Problem Author:Ivan burmistrov
Problem Source:Ural regional school programming contest 2009

Tags:None

Question

This question requires that a specified positive integer (less than 105) be divided into some different positive integers (must also be less than 105) and the numbers of these positive integers are exactly equal to the specified positive integer.

Answer

The C # language is as follows:

using System;// http://acm.timus.ru/problem.aspx?space=1&num=1727static class Timus{  static void Main()  {    int n = int.Parse(Console.ReadLine());    int count = Compute(ref n), tops = count;    for (int i = 0; n > 9; i++, n -= 10) count++;    int tens = count - tops;    Console.WriteLine(count += ((n > 0) ? 1 : 0));    for (int i = 99999; tops-- > 0; i--) Console.Write(i + " ");    while (tens-- > 0) Console.Write("19 28 37 46 ".Substring(tens * 3, 3));    if (n > 0) Console.Write(n);  }    static int Compute(ref int n)  {    int count = 0;    for (int sum = 45; n >= 45; )    {      n -= sum--;      if (++count % 10 == 0) sum += 9;      if (count % 100 == 0) sum += 9;      if (count % 1000 == 0) sum += 9;     }    return count;  }}

The analysis is as follows:

  • First, start from 99999 and end down until n is less than 45. Note that the sum of the numbers in 99999 is exactly 45, which is the sum of the largest numbers.
  • Then, the sum of the four digits 19, 28, 37, and 46 is exactly 10.
  • Finally, N must be less than 10. If n is not zero, n is output directly.
  • The compute method is the process of counting down from 99999. The numbers are quite regular, that is, the number decreases from 45.

In addition, you can use the random algorithm as follows:

using System;using System.Linq;using System.Collections.Generic;// http://acm.timus.ru/problem.aspx?space=1&num=1727static class Timus{  static void Main()  {    var n = int.Parse(Console.ReadLine());    var set = Compute(ref n);    var count = set.Count;    for (var i = 0; n > 9; i++, n -= 10) count++;    var tens = count - set.Count;    Console.WriteLine(count += ((n > 0) ? 1 : 0));    foreach (var i in set) Console.Write(i + " ");    while (tens-- > 0) Console.Write("19 28 37 46 ".Substring(tens * 3, 3));    if (n > 0) Console.Write(n);    Console.WriteLine();  }    static HashSet<int> Compute(ref int n)  {    var set = new HashSet<int>();    for (var rand = new Random(); n >= 45; )    {      var k = rand.Next(100, 100000);      if (set.Add(k)) n -= k.ToString().Select(x => x - '0').Sum();    }    return set;  }}

This program uses hashset to save the randomly generated positive integers so that repeated positive integers can be discarded and these integers can be output later. Note that the 28th rows use the select and sum extension methods to sum the numbers.

The first program runs for 0.14 seconds, and the second program runs for 0.156 seconds.

Let's perform a test:

using System;using System.Linq;using System.Diagnostics;static class Tester{  static void Main(string[] args)  {    var n = (args.Length == 0) ? 100000000 : int.Parse(args[0]);    Run(n, DigitsSum1);    Run(n, DigitsSum2);  }    static void Run(int n, Func<int, int> func)  {    var timer = Stopwatch.StartNew();    long sum = 0;    for (var i = 0; i <= n; i++) sum += func(i);    timer.Stop();    Console.WriteLine("{0} {1:N0} {2:N0}", timer.Elapsed, n, sum);  }    static int DigitsSum1(this int n)  {    var sum = 0;    for (; n > 0; n /= 10) sum += n % 10;    return sum;  }    static int DigitsSum2(this int n)  {    return n.ToString().Select(x => x - '0').Sum();  }}

The running result of this program is as follows:

E:\work>tester00:00:20.3631972 100,000,000 3,600,000,00100:00:58.6052102 100,000,000 3,600,000,001

It seems that using LINQ For summation will not be slow.

Returned directory

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.