[洛谷]P1591 階乘數碼 (#高精度 -1.2)
阿新 • • 發佈:2018-12-11
題目描述
求n!中某個數碼出現的次數。
輸入輸出格式
輸入格式:
第一行為t(≤10),表示資料組數。接下來t行,每行一個正整數n(≤1000)和數碼a。
輸出格式:
對於每組資料,輸出一個整數,表示n!中a出現的次數。
輸入輸出樣例
輸入樣例#1
2 5 2 7 0
輸出樣例#1
1 2
思路
過載:0分TLE
#include<string> #include<iostream> #include<iosfwd> #include<cmath> #include<cstring> #include<stdlib.h> #include<stdio.h> #include<cstring> #define MAX_L 10005 //最大長度,可以修改 using namespace std; class bign//大整數類模版,用過載寫的(因為我懶得打高精度了(逃)) { public: int len, s[MAX_L];//數的長度,記錄陣列 //建構函式 bign(); bign(const char*); bign(int); bool sign;//符號 1正數 0負數 string toStr() const;//轉化為字串,主要是便於輸出 friend istream& operator>>(istream &,bign &);//過載輸入流 friend ostream& operator<<(ostream &,bign &);//過載輸出流 //過載複製 bign operator=(const char*); bign operator=(int); bign operator=(const string); //過載各種比較 bool operator>(const bign &) const; bool operator>=(const bign &) const; bool operator<(const bign &) const; bool operator<=(const bign &) const; bool operator==(const bign &) const; bool operator!=(const bign &) const; //過載四則運算 bign operator+(const bign &) const; bign operator++(); bign operator++(int); bign operator+=(const bign&); bign operator-(const bign &) const; bign operator--(); bign operator--(int); bign operator-=(const bign&); bign operator*(const bign &)const; bign operator*(const int num)const; bign operator*=(const bign&); bign operator/(const bign&)const; bign operator/=(const bign&); //四則運算的衍生運算 bign operator%(const bign&)const;//取模(餘數) bign factorial()const;//階乘 bign Sqrt()const;//整數開根(向下取整) bign pow(const bign&)const;//次方 //一些亂亂的函式 void clean(); ~bign(); }; #define max(a,b) a>b ? a : b #define min(a,b) a<b ? a : b bign::bign() { memset(s, 0, sizeof(s)); len = 1; sign = 1; } bign::bign(const char *num) { *this = num; } bign::bign(int num) { *this = num; } string bign::toStr() const { string res; res = ""; for (int i = 0; i < len; i++) res = (char)(s[i] + '0') + res; if (res == "") res = "0"; if (!sign&&res != "0") res = "-" + res; return res; } istream &operator>>(istream &in, bign &num) { string str; in>>str; num=str; return in; } ostream &operator<<(ostream &out, bign &num) { out<<num.toStr(); return out; } bign bign::operator=(const char *num) { memset(s, 0, sizeof(s)); char a[MAX_L] = ""; if (num[0] != '-') strcpy(a, num); else for (int i = 1; i < strlen(num); i++) a[i - 1] = num[i]; sign = !(num[0] == '-'); len = strlen(a); for (int i = 0; i < strlen(a); i++) s[i] = a[len - i - 1] - 48; return *this; } bign bign::operator=(int num) { char temp[MAX_L]; sprintf(temp, "%d", num); *this = temp; return *this; } bign bign::operator=(const string num) { const char *tmp; tmp = num.c_str(); *this = tmp; return *this; } bool bign::operator<(const bign &num) const { if (sign^num.sign) return num.sign; if (len != num.len) return len < num.len; for (int i = len - 1; i >= 0; i--) if (s[i] != num.s[i]) return sign ? (s[i] < num.s[i]) : (!(s[i] < num.s[i])); return !sign; } bool bign::operator>(const bign&num)const { return num < *this; } bool bign::operator<=(const bign&num)const { return !(*this>num); } bool bign::operator>=(const bign&num)const { return !(*this<num); } bool bign::operator!=(const bign&num)const { return *this > num || *this < num; } bool bign::operator==(const bign&num)const { return !(num != *this); } bign bign::operator+(const bign &num) const { if (sign^num.sign) { bign tmp = sign ? num : *this; tmp.sign = 1; return sign ? *this - tmp : num - tmp; } bign result; result.len = 0; int temp = 0; for (int i = 0; temp || i < (max(len, num.len)); i++) { int t = s[i] + num.s[i] + temp; result.s[result.len++] = t % 10; temp = t / 10; } result.sign = sign; return result; } bign bign::operator++() { *this = *this + 1; return *this; } bign bign::operator++(int) { bign old = *this; ++(*this); return old; } bign bign::operator+=(const bign &num) { *this = *this + num; return *this; } bign bign::operator-(const bign &num) const { bign b=num,a=*this; if (!num.sign && !sign) { b.sign=1; a.sign=1; return b-a; } if (!b.sign) { b.sign=1; return a+b; } if (!a.sign) { a.sign=1; b=bign(0)-(a+b); return b; } if (a<b) { bign c=(b-a); c.sign=false; return c; } bign result; result.len = 0; for (int i = 0, g = 0; i < a.len; i++) { int x = a.s[i] - g; if (i < b.len) x -= b.s[i]; if (x >= 0) g = 0; else { g = 1; x += 10; } result.s[result.len++] = x; } result.clean(); return result; } bign bign::operator * (const bign &num)const { bign result; result.len = len + num.len; for (int i = 0; i < len; i++) for (int j = 0; j < num.len; j++) result.s[i + j] += s[i] * num.s[j]; for (int i = 0; i < result.len; i++) { result.s[i + 1] += result.s[i] / 10; result.s[i] %= 10; } result.clean(); result.sign = !(sign^num.sign); return result; } bign bign::operator*(const int num)const { bign x = num; bign z = *this; return x*z; } bign bign::operator*=(const bign&num) { *this = *this * num; return *this; } bign bign::operator /(const bign&num)const { bign ans; ans.len = len - num.len + 1; if (ans.len < 0) { ans.len = 1; return ans; } bign divisor = *this, divid = num; divisor.sign = divid.sign = 1; int k = ans.len - 1; int j = len - 1; while (k >= 0) { while (divisor.s[j] == 0) j--; if (k > j) k = j; char z[MAX_L]; memset(z, 0, sizeof(z)); for(int i = j; i >= k; i--) z[j - i] = divisor.s[i] + '0'; bign dividend = z; if (dividend < divid) { k--; continue; } int key = 0; while (divid*key<=dividend) key++; key--; ans.s[k] = key; bign temp = divid*key; for (int i = 0; i < k; i++) temp = temp * 10; divisor = divisor - temp; k--; } ans.clean(); ans.sign = !(sign^num.sign); return ans; } bign bign::operator/=(const bign&num) { *this = *this / num; return *this; } bign bign::operator%(const bign& num)const { bign a = *this, b = num; a.sign = b.sign = 1; bign result, temp = a / b*b; result = a - temp; result.sign = sign; return result; } bign bign::pow(const bign& num)const { bign result = 1; for (bign i = 0; i < num; i++) result = result*(*this); return result; } bign bign::factorial()const { bign result = 1; for(bign i=1;i<=*this;i++) result *= i; return result; } void bign::clean() { if (len == 0) len++; while (len > 1 && s[len - 1] == '\0') len--; } bign bign::Sqrt()const { if(*this<0)return -1; if(*this<=1)return *this; bign l=0,r=*this,mid; while(r-l>1) { mid=(l+r)/2; if(mid*mid>*this) r=mid; else l=mid; } return l; } bign::~bign() { } bign n,m,t,i,j,k,s(1),res,ans; int main() { ios::sync_with_stdio(false); cin.tie(0); cin>>t; for(k=1;k<=t;k++) { cin>>n>>m; for(i=1;i<=n;i++) { s=s*i; } while(s!=0) { res=s%10; if(res==m) { ans++; } s=s/10; } cout<<ans<<endl; s=1; ans=0; } return 0; }
普通做法:AC
#include <stdio.h> #include <iostream> #include <string.h> int a[20001],n,m,t,s;//n!,m是數碼,t是有t位,s存答案,a陣列是一串高精度數 using namespace std; inline int multe(int n) { register int i,j; for(i=1;i<=t;i++)//先讓a陣列的每一位都能乘以n { a[i]*=n; } for(i=1;i<=t;i++)//從1到位數進行迴圈 { if(a[i]>9)//如果需要進位 { a[i+1]+=a[i]/10;//進位 a[i]%=10; if(i+1>t)//如果現在是最高位 { t++;//位+1 } } } return 0; } inline void lxydl()//lxy大佬太強啦! { for(register int i=t;i>=1;i--)//這個倒或正可能無所謂吧,但是高精度後出來的數字都是倒序的,那我們也倒敘 { if(a[i]==m)//如果當前的a[i]等於數 { s++;//算符+1 } } } int main() { ios::sync_with_stdio(false); cin.tie(0); register int i,j,l;//l是l組資料 cin>>l; while(l--) { cin>>n>>m; s=0;//答案初始化 memset(a,0,sizeof(a));//清除快取 a[1]=1;//假設第一位數為1,否則0乘任何數都是0 t=1;//至少有1位 for(i=1;i<=n;i++)//求n! { multe(i);//a陣列乘以i } lxydl();//找n!裡有多少m這個數 cout<<s<<endl;//輸出答案 } return 0; }