UVa 10916 factstone Benchmark: Techniques for handling math and factorial

Source: Internet
Author: User
Tags benchmark printf time limit

10916-factstone Benchmark

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_problem &problem=1857

Amtel has announced that it'll release a 128-bit computer chip by, a 256-bit computer by 2020, and, Continuin G its strategy of doubling the word-size every ten years. (Amtel released a 64-bit computer in), a 32-bit computer in 1990, a 16-bit computer in 1980, a 8-bit computer in 1970 , and a 4-bit computer, its-alpha, in 1960.)

Amtel would use a new benchmark-the factstone -to advertise the vastly improved the capacity of it new chips. The Factstone rating is defined to being largest integern such that n! can be represented as a n unsigned integer in a computer word.

Given a year 1960≤y≤2160, what would be the Factstone rating of Amtel ' s most recently-released?

There are several test cases. For each test case, the there is one line of input containing y. A line containing 0 follows of the last Test case. For each test case, output a line giving the Factstone rating.

Sample Input

1960
1981
0

Output for Sample Input

3
8

Train of thought: The number of bytes k = (year-1940)/10, the problem is turned into n! < 2 ^ K < (n + 1)!, if the simple simulation will overflow, so we take the logarithm on both sides, because log (a*b) = log (a) + log (b), so log (n!) = SUM (log (i)), (1<= i <= n ), just find the smallest sum (log (i)) > K * log (2), the answer is i-1.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Complete code:

/*0.286s*/
  
#include <cstdio>
#include <cmath>
const double Log_2=log (2.0);
  
int main (void)
{
    int year;
    while (scanf ("%d", &year), year)
    {
        int n = (year-1940)/ten;
        Double k = POW (2, N) * log_2, sum = 0;
        for (int i = 1;; i++)
        {
            sum = log (i);
            if (Sum > K)
            {
                printf ("%d\n", i-1);
                break;
            }
    }} return 0;
}

Play table:

/*0.012s*/
  
#include <cstdio>
  
const int ANS[21] =
{
    3, 5, 8, A,
    170,
    536, 966, 1754, 3210, 5910, 10944, 20366, 38064, 71421, 134480, 254016
};
  
int main (void)
{
    int year;
    while (scanf ("%d", &year), year)
        printf ("%d\n", ans[(year-1960)/ten);
}

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.