AOJ 787.阶乘中找数

193

题目



Description  


问题描述:统计n阶乘中数字p的个数



Input  


多组数据,每组数据只有一行,为两个正整数n,p (n<=800,0<=p<=9)


Output  


输出为两行,一行为n!, 另一行为n! 中数字p的个数


Sample Input  


10 8


Sample Output  


3628800
2


题解


第一次把 800 看成 8000 ,优化高精度优化了好久


代码


/*
By:OhYee
Github:OhYee
Email:oyohyee@oyohyee.com
Blog:http://www.cnblogs.com/ohyee/

かしこいかわいい?
エリーチカ!
要写出来Хорошо的代码哦~
*/

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <stack>
using namespace std;

#define REP(n) for(int o=0;o<n;o++)

class bigNumber {
public:
int num[10001];
bigNumber() {
init();
}
void init() {
memset(num,0,sizeof(num));
}
bigNumber operator = (const bigNumber& rhs) {
init();
REP(rhs.num[0] + 1)num[o] = rhs.num[o];
return *this;
}
bigNumber operator = (long long rhs) {
init();
int i = 1;
while(rhs) {
num[i] = rhs % 10;
rhs /= 10;
i++;
}
num[0] = i - 1;
return *this;
}
bool operator < (const bigNumber rhs)const {
if(num[0] != rhs.num[0])return (num[0]<rhs.num[0]);
REP(num[0]) {
int temp = num[0] - o;
if(num[temp] != rhs.num[temp])return (num[temp]<rhs.num[temp]);
}
return 0;
}
bool operator > (const bigNumber rhs)const {
if(num[0] != rhs.num[0])return (num[0]>rhs.num[0]);
REP(num[0]) {
int temp = num[0] - o;
if(num[temp] != rhs.num[temp])return (num[temp]>rhs.num[temp]);
}
return 0;
}
bool operator == (const bigNumber rhs)const {
return !(*this>rhs || *this<rhs);
}
bool operator <= (const bigNumber rhs)const {
return *this<rhs || *this == rhs;
}
bigNumber operator + (const bigNumber rhs)const {
bigNumber temp;
int len;
len = num[0]>rhs.num[0] ? num[0] : rhs.num[0];
len++;
REP(len) {
temp.num[o + 1] += num[o + 1] + rhs.num[o + 1];
temp.num[o + 2] += temp.num[o + 1] / 10;
temp.num[o + 1] %= 10;
}
REP(len) {
if(temp.num[len - o] != 0) {
temp.num[0] = len - o;
break;
}
}
return temp;
}
bigNumber operator + (const long long rhs)const {
bigNumber temp1,temp2;
temp1 = *this;
temp2 = rhs;
return temp1 + temp2;
}
bigNumber operator * (const bigNumber rhs)const {
bigNumber temp;
int len;
len = num[0] + rhs.num[0];
//len++;
for(int i = 1;i <= num[0];i++) {
for(int j = 1;j <= rhs.num[0];j++) {
temp.num[i + j - 1] += num[i] * rhs.num[j];
temp.num[i + j] += temp.num[i + j - 1] / 10;
temp.num[i + j - 1] %= 10;
}
}
REP(len) {
if(temp.num[len - o] != 0) {
temp.num[0] = len - o;
break;
}
}
return temp;
}
bigNumber operator * (const long long rhs)const {
bigNumber temp1,temp2;
temp1 = *this;
temp2 = rhs;
return temp1 * temp2;
}
bigNumber operator - (const bigNumber rhs)const {
bigNumber temp,a,b;
temp = max(*this,rhs);
b = min(*this,rhs);
a = temp;
temp = 0;
int len = a.num[0];
REP(len) {
temp.num[o + 1] += 10 + a.num[o + 1] - b.num[o + 1];
temp.num[o + 2]--;
temp.num[o + 2] += temp.num[o + 1] / 10;
temp.num[o + 1] %= 10;
}
REP(len) {
if(temp.num[len - o] != 0) {
temp.num[0] = len - o;
break;
}
}
return temp;
}
bigNumber operator - (const long long rhs)const {
bigNumber temp1,temp2;
temp1 = *this;
temp2 = rhs;
return temp1 - temp2;
}
bigNumber operator / (const bigNumber rhs)const {
bigNumber a;
int it = num[0];

bigNumber d;
bigNumber c;

while(it>0) {
a = (d * 10) + num[it];
c = c * 10;

int t;
REP(9) {
if(a < rhs * (o + 1)) {
t = o;
break;
}
t = 9;
}
c = c + t;
d = a - rhs*t;
it--;
}
return c;
}
bigNumber operator / (const long long rhs)const {
bigNumber temp1,temp2;
temp1 = *this;
temp2 = rhs;
return temp1 / temp2;
}
bigNumber operator % (const bigNumber rhs)const {
bigNumber a;
int it = num[0];

bigNumber d;
bigNumber c;



while(it>0) {

a = (d * 10) + num[it];
c = c * 10;

int t;
REP(9) {
if(a < rhs * (o + 1)) {
t = o;
break;
}
t = 9;
}
c = c + t;
d = a - rhs*t;
it--;
}
return d;
}
bigNumber operator % (const long long rhs)const {
bigNumber temp1,temp2;
temp1 = *this;
temp2 = rhs;
return temp1 % temp2;
}
void p() {
if(num[0] == 0)
printf("0");
REP(num[0]) {
printf("%d",num[num[0] - o]);
}
}
};

bigNumber ans;

bool Do() {
int n,p;
if(scanf("%d%d",&n,&p) == EOF)
return false;

ans = 1;
int cnt = 0;

for(int i = 2;i <= n;i++) {
ans = ans * i;
}

REP(ans.num[0]) {
printf("%d",ans.num[ans.num[0] - o]);
if(ans.num[ans.num[0] - o] == p)cnt++;
}

printf("\n%d\n",cnt);
return true;
}

int main() {
while(Do());
return 0;
}
发布评论
  • 点击查看/关闭被识别为广告的评论