UVa 324 Factorial Frequencies:高精度

324 - Factorial Frequencies

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=260

In an attempt to bolster her sagging palm-reading business, Madam Phoenix has decided to offer several numerological treats to her customers. She has been able to convince them that the frequency of occurrence of the digits in the decimal representation of factorials bear witness to their futures. Unlike palm-reading, however, she can't just conjure up these frequencies, so she has employed you to determine these values.

Recall that the definition of n! (that is, n factorial) is just . As she expects to use either the day of the week, the day of the month, or the day of the year as the value ofn, you must be able to determine the number of occurrences of each decimal digit in numbers as large as 366 factorial (366!), which has 781 digits.

Input and Output

The input data for the program is simply a list of integers for which the digit counts are desired. All of these input values will be less than or equal to 366 and greater than 0, except for the last integer, which will be zero. Don't bother to process this zero value; just stop your program at that point. The output format isn't too critical, but you should make your program produce results that look similar to those shown below.

Madam Phoenix will be forever (or longer) in your debt; she might even give you a trip if you do your job well!

Sample Input

3
8
100
0

Sample Output

3! --
   (0)    0    (1)    0    (2)    0    (3)    0    (4)    0
   (5)    0    (6)    1    (7)    0    (8)    0    (9)    0
8! --
   (0)    2    (1)    0    (2)    1    (3)    1    (4)    1
   (5)    0    (6)    0    (7)    0    (8)    0    (9)    0
100! --
   (0)   30    (1)   15    (2)   19    (3)   10    (4)   10
   (5)   14    (6)   19    (7)    7    (8)   14    (9)   20

完整代码:

/*0.022s*/

#include<bits/stdc++.h>
using namespace std;
const int maxn = 800;  

char outputstr[maxn];///此字符串为bign中s的拷贝,用于输出
int dig[10];  

struct bign
{
    int len, s[maxn];  

    bign()
    {
        memset(s, 0, sizeof(s));
        len = 1;
    }  

    bign(int num)
    {
        *this = num;
    }  

    bign(const char* num)
    {
        *this = num;
    }  

    bign operator = (const int num)
    {
        char s[maxn];
        sprintf(s, "%d", num);
        *this = s;
        return *this;
    }  

    bign operator = (const char* num)
    {
        len = strlen(num);
        for (int i = 0; i < len; i++) s[i] = num[len - i - 1] & 15;
        return *this;
    }  

    ///输出
    const char* str() const
    {
        if (len)
        {
            for (int i = 0; i < len; i++)
                outputstr[i] = '0' + s[len - i - 1];
            outputstr[len] = '\0';
        }
        else strcpy(outputstr, "0");
        return outputstr;
    }  

    ///去前导零
    void clean()
    {
        while (len > 1 && !s[len - 1]) len--;
    }  

    ///加
    bign operator + (const bign& b) const
    {
        bign c;
        c.len = 0;
        for (int i = 0, g = 0; g || i < max(len, b.len); i++)
        {
            int x = g;
            if (i < len) x += s[i];
            if (i < b.len) x += b.s[i];
            c.s[c.len++] = x % 10;
            g = x / 10;
        }
        return c;
    }  

    ///减
    bign operator - (const bign& b) const
    {
        bign c;
        c.len = 0;
        for (int i = 0, g = 0; i < len; i++)
        {
            int x = s[i] - g;
            if (i < b.len) x -= b.s[i];
            if (x >= 0) g = 0;
            else
            {
                g = 1;
                x += 10;
            }
            c.s[c.len++] = x;
        }
        c.clean();
        return c;
    }  

    ///乘
    bign operator * (const bign& b) const
    {
        bign c;
        c.len = len + b.len;
        for (int i = 0; i < len; i++)
            for (int j = 0; j < b.len; j++)
                c.s[i + j] += s[i] * b.s[j];
        for (int i = 0; i < c.len - 1; i++)
        {
            c.s[i + 1] += c.s[i] / 10;
            c.s[i] %= 10;
        }
        c.clean();
        return c;
    }  

    ///除
    bign operator / (const bign &b) const
    {
        bign ret, cur = 0;
        ret.len = len;
        for (int i = len - 1; i >= 0; i--)
        {
            cur = cur * 10;
            cur.s[0] = s[i];
            while (cur >= b)
            {
                cur -= b;
                ret.s[i]++;
            }
        }
        ret.clean();
        return ret;
    }  

    ///模、余
	///本文URL地址:http://www.bianceng.cn/Programming/sjjg/201410/45518.htm
    bign operator % (const bign &b) const
    {
        bign c = *this / b;
        return *this - c * b;
    }  

    bool operator < (const bign& b) const
    {
        if (len != b.len) return len < b.len;
        for (int i = len - 1; i >= 0; i--)
            if (s[i] != b.s[i]) return s[i] < b.s[i];
        return false;
    }  

    bool operator > (const bign& b) const
    {
        return b < *this;
    }  

    bool operator <= (const bign& b) const
    {
        return !(b < *this);
    }  

    bool operator >= (const bign &b) const
    {
        return !(*this < b);
    }  

    bool operator == (const bign& b) const
    {
        return !(b < *this) && !(*this < b);
    }  

    bool operator != (const bign &a) const
    {
        return *this > a || *this < a;
    }  

    bign operator += (const bign &a)
    {
        *this = *this + a;
        return *this;
    }  

    bign operator -= (const bign &a)
    {
        *this = *this - a;
        return *this;
    }  

    bign operator *= (const bign &a)
    {
        *this = *this * a;
        return *this;
    }  

    bign operator /= (const bign &a)
    {
        *this = *this / a;
        return *this;
    }  

    bign operator %= (const bign &a)
    {
        *this = *this % a;
        return *this;
    }
} a[367];  

int main()
{
    int i, n, len;
    a[1] = 1;
    for (i = 2; i < 367; ++i)
        a[i] = a[i - 1] * i;
    while (scanf("%d", &n), n)
    {
        memset(dig, 0, sizeof(dig));
        len = strlen(a[n].str());
        for (i = 0; i < len; ++i)
            ++dig[outputstr[i] & 15];
        printf("%d! --\n", n);
        for (i = 0; i < 10; ++i)
        {
            printf("   (%d)%5d", i, dig[i]);
            if (i == 4) putchar(10);
        }
        putchar(10);
    }
    return 0;
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索int
, return
, this
, const
, operator
, len
outputstring
factorial、factorial函数、factorial design、matlab factorial、factorial label,以便于您获取更多的相关知识。

时间: 2024-11-05 06:18:46

UVa 324 Factorial Frequencies:高精度的相关文章

UVa 424 Integer Inquiry (高精度)

424 - Integer Inquiry Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=97&page=show_problem&problem=365 One of the first users of BIT's new supercomputer was Chip Diller. He extended h

UVa 10106 Product:高精度

10106 - Product Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=97&page=show_problem&problem=1047 The Problem The problem is to multiply two integers X, Y. (0<=X,Y<10250) The In

POJ题目分类

初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      (4)递推.      (5)构造法.(poj3295)      (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法:      (1)图的深度优先遍历和广度优先遍历.      (2)最短路径算法(dijkstra,bellman-ford

poj分类

初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      (4)递推.      (5)构造法.(poj3295)      (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法:      (1)图的深度优先遍历和广度优先遍历.      (2)最短路径算法(dijkstra,bellman-ford

ACM练级

一般要做到50行以内的程序不用调试.100行以内的二分钟内调试成功.acm主要是考算法的 ,主要时间是花在思考算法上,不是花在写程序与debug上.  下面给个计划你练练: 第一阶段: 练经典常用算法,下面的每个算法给我打上十到二十遍,同时自己精简代码, 因为太常用,所以要练到写时不用想,10-15分钟内打完,甚至关掉显示器都可以把程序打 出来.  1.最短路(Floyd.Dijstra,BellmanFord)  2.最小生成树(先写个prim,kruscal要用并查集,不好写)  3.大数(

poj 题型分类

主流算法: 1.搜索 //回溯 2.DP(动态规划) 3.贪心 4.图论 //Dijkstra.最小生成树.网络流 5.数论 //解模线性方程 6.计算几何 //凸壳.同等安置矩形的并的面积与周长 7.组合数学 //Polya定理 8.模拟 9.数据结构 //并查集.堆 10.博弈论 1. 排序 1423, 1694, 1723, 1727, 1763, 1788, 1828, 1838, 1840, 2201, 2376, 2377, 2380, 1318, 1877, 1928, 1971,

hduoj题目分类

基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029.1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093.1094.1095.1096.1097.1098.1106.1108.1157.1163.1164.1170.1194.1196.1197.1201.1202.1205.1219.1234.1235.1236.1248.1

UVa 623 500! (高精度阶乘)

623 - 500! Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=564 In these days you can more and more often happen to see programs which perform some useful

UVa 10247 Complete Tree Labeling:组合数学&amp;amp;高精度

10247 - Complete Tree Labeling Time limit: 15.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1188 A complete k-ary tree is a k-ary tree in which all leaves have same d