高精度为什么WA,样例过了

P1591 阶乘数码

Chen_Three @ 2024-07-17 11:04:30

#include<bits/stdc++.h>
using namespace std;
#include<bits/stdc++.h>
using namespace std;
const int N = 10000 ;
char S[N] ;
char T[N] ;
struct bigNumber{
    int len , x[N] ;
    bigNumber( ) {len = 1 ; memset(x, 0 , sizeof(x)) ;}
    void print() {
        for (int i = len ;i;i--) printf("%d",x[i]) ;puts("") ;
    }
} a,b,c;//f[N][N] , s[N][N] , a,b,c;

inline bool operator <(bigNumber a, bigNumber b) {
    if (a.len != b.len ) return a.len< b.len ;
    for (int i = a.len;i ;i--) 
        if (a.x[i] != b.x[i]) return a.x[i] <b.x[i] ;
    return 0 ;
}

inline bool operator <=(bigNumber a, bigNumber b) { 
    if (a.len != b.len ) return a.len< b.len ;
    for (int i = a.len;i ;i--) 
        if (a.x[i] < b.x[i]) return 1 ;
        else if (a.x[i] >b.x[i]) return 0 ;
    return 1 ;
}

bigNumber fix(bigNumber a ) {
    for (int i = 1;i< a.len;++ i) {
        if (a.x[i] <0) a.x[i]+= 10 , a.x[i+1] -- ;
        a.x[i+1] += a.x[i] / 10 ;
        a.x[i] %= 10 ;
    }
    while (a.x[a.len] >=10 ) {
        a.x[a.len + 1] += a.x[a.len] / 10 ;
        a.x[a.len] %= 10 ; a.len ++ ;
    }
    while (!a.x[a.len] && a.len>1) a.len-- ;
    return a ;  
}

inline bigNumber operator *(bigNumber a , int b) {
    for (int i = 1;i<= a.len ;++ i ) a.x[i] *= b ;
    return fix(a) ;
}
inline bigNumber operator *(bigNumber a , bigNumber b) {
    bigNumber c ;   
    c.len = a.len + b.len -1 ; 
    for (int i = 1;i<= a.len ;++ i) 
        for (int j = 1;j<=b.len ; ++j) 
            c.x[i+j-1] += a.x[i] * b.x[j] ;
    return fix(c) ;
}

inline bigNumber operator +(bigNumber a , int b) {
    a.x[1] += b ;
    return fix(a) ;
}

inline bigNumber operator +(bigNumber a , bigNumber b) {
    bigNumber c ;
    c.len = max(a.len, b.len ) ;
    for (int i = 1;i<= c.len ;++ i) c.x[i] = a.x[i] + b.x[i] ;
    return fix(c) ;
}

inline bigNumber operator -(bigNumber a , int b) {
    a.x[1] -= b ;
    return fix(a) ;
}

inline bigNumber operator -(bigNumber a , bigNumber b) {
    bigNumber c ;
    c.len = max(a.len, b.len ) + 1 ;
    for (int i = 1;i<= c.len ;++ i) c.x[i] = a.x[i] - b.x[i] ;
    return fix(c) ;
}

inline bigNumber operator /(bigNumber a, int y) 
{
    int x = 0 ;
    bigNumber c ;
    c.len = a.len ;
    for (int i = c.len; i>0; --i) {
        c.x[i] = (x* 10 + a.x[i]) / y ; 
        x = (x* 10+ a.x[i]) % y ; 
    }       
    return fix(c) ;
} 

inline bigNumber operator /(bigNumber a, bigNumber b) {
    bigNumber c ;   
    int s= 0 ;
    while (a.len >b.len) b= b * 10 , s++ ;
    while (s>=0) {
        c= c * 10 ; 
        while (b <= a) c=c+1 , a = a- b;
        b= b / 10  , s-- ; 
    }
    return fix(c) ; 
}

inline bigNumber operator %(bigNumber a, int y) 
{
    bigNumber c = a / y ;
    a = a - c * y ;
    return fix(a) ; 
}

inline bigNumber operator %(bigNumber a, bigNumber b) 
{
    int s= 0 ;
    while (a.len >b.len) b= b * 10 , s++ ;
    while (s>=0) {
        while (b <= a) a = a - b;   
        b= b / 10  , s-- ; 
    }
    return fix(a) ; 
}

inline bigNumber poww(int m, int p) 
{
    a.x[1] = 1 ;
    b.x[1] = m ;
    while (p) {
        if (p & 1) a= a * b;
        p= p>> 1; 
        b= b* b ;
    }
    return fix(a) ;
}

bigNumber findv(int ind,bigNumber a){
    bigNumber cnt;
    for(int i = 1;i <= a.len; i++){
        if(a.x[i]==ind){
            cnt=cnt+1;
        }
    }
    return cnt;
}
int main(){
    bigNumber b,c;
    int T,index,a;cin >> T;
    while(T--){
        c.x[1]=1;
        cin >> a;
        cin >> index;
        for(int i=1;i<=a;i+=1){
            c=c*i;
        }
        for(int i = 1;i<=findv(index,c).len;i++){
            cout <<findv(index,c).x[i];
        }
        cout << endl;
    }

}

by Kete @ 2024-07-18 10:33:01

温馨提示

  1. 第一、二行的前导与第三、四行的重复删去任意两行即可
  2. Puts函数早已在C++14中删除 请用C++11

by Chen_Three @ 2024-07-18 12:48:37

@shengzhe0227

#include<bits/stdc++.h>
using namespace std;
const int N = 10000 ;
char S[N] ;
char T[N] ;
struct bigNumber{
    int len , x[N] ;
    bigNumber( ) {len = 1 ; memset(x, 0 , sizeof(x)) ;}
    void print() {
        for (int i = len ;i;i--) printf("%d",x[i]) ;cout << endl;
    }
} a,b,c;//f[N][N] , s[N][N] , a,b,c;

inline bool operator <(bigNumber a, bigNumber b) {
    if (a.len != b.len ) return a.len< b.len ;
    for (int i = a.len;i ;i--) 
        if (a.x[i] != b.x[i]) return a.x[i] <b.x[i] ;
    return 0 ;
}

inline bool operator <=(bigNumber a, bigNumber b) { 
    if (a.len != b.len ) return a.len< b.len ;
    for (int i = a.len;i ;i--) 
        if (a.x[i] < b.x[i]) return 1 ;
        else if (a.x[i] >b.x[i]) return 0 ;
    return 1 ;
}

bigNumber fix(bigNumber a ) {
    for (int i = 1;i< a.len;++ i) {
        if (a.x[i] <0) a.x[i]+= 10 , a.x[i+1] -- ;
        a.x[i+1] += a.x[i] / 10 ;
        a.x[i] %= 10 ;
    }
    while (a.x[a.len] >=10 ) {
        a.x[a.len + 1] += a.x[a.len] / 10 ;
        a.x[a.len] %= 10 ; a.len ++ ;
    }
    while (!a.x[a.len] && a.len>1) a.len-- ;
    return a ;  
}

inline bigNumber operator *(bigNumber a , int b) {
    for (int i = 1;i<= a.len ;++ i ) a.x[i] *= b ;
    return fix(a) ;
}
inline bigNumber operator *(bigNumber a , bigNumber b) {
    bigNumber c ;   
    c.len = a.len + b.len -1 ; 
    for (int i = 1;i<= a.len ;++ i) 
        for (int j = 1;j<=b.len ; ++j) 
            c.x[i+j-1] += a.x[i] * b.x[j] ;
    return fix(c) ;
}

inline bigNumber operator +(bigNumber a , int b) {
    a.x[1] += b ;
    return fix(a) ;
}

inline bigNumber operator +(bigNumber a , bigNumber b) {
    bigNumber c ;
    c.len = max(a.len, b.len ) ;
    for (int i = 1;i<= c.len ;++ i) c.x[i] = a.x[i] + b.x[i] ;
    return fix(c) ;
}

inline bigNumber operator -(bigNumber a , int b) {
    a.x[1] -= b ;
    return fix(a) ;
}

inline bigNumber operator -(bigNumber a , bigNumber b) {
    bigNumber c ;
    c.len = max(a.len, b.len ) + 1 ;
    for (int i = 1;i<= c.len ;++ i) c.x[i] = a.x[i] - b.x[i] ;
    return fix(c) ;
}

inline bigNumber operator /(bigNumber a, int y) 
{
    int x = 0 ;
    bigNumber c ;
    c.len = a.len ;
    for (int i = c.len; i>0; --i) {
        c.x[i] = (x* 10 + a.x[i]) / y ; 
        x = (x* 10+ a.x[i]) % y ; 
    }       
    return fix(c) ;
} 

inline bigNumber operator /(bigNumber a, bigNumber b) {
    bigNumber c ;   
    int s= 0 ;
    while (a.len >b.len) b= b * 10 , s++ ;
    while (s>=0) {
        c= c * 10 ; 
        while (b <= a) c=c+1 , a = a- b;
        b= b / 10  , s-- ; 
    }
    return fix(c) ; 
}

inline bigNumber operator %(bigNumber a, int y) 
{
    bigNumber c = a / y ;
    a = a - c * y ;
    return fix(a) ; 
}

inline bigNumber operator %(bigNumber a, bigNumber b) 
{
    int s= 0 ;
    while (a.len >b.len) b= b * 10 , s++ ;
    while (s>=0) {
        while (b <= a) a = a - b;   
        b= b / 10  , s-- ; 
    }
    return fix(a) ; 
}

inline bigNumber poww(int m, int p) 
{
    a.x[1] = 1 ;
    b.x[1] = m ;
    while (p) {
        if (p & 1) a= a * b;
        p= p>> 1; 
        b= b* b ;
    }
    return fix(a) ;
}

int findv(int ind,bigNumber a){
    int cnt = 0;
    for(int i = 1;i <= a.len; i++){
        if(a.x[i]==ind){
            cnt=cnt+1;
        }
    }
    return cnt;
}
int main(){
    bigNumber b,c;
    int T,index,a;cin >> T;
    while(T--){
        c.x[1]=1;
        cin >> a;
        cin >> index;
        for(int i=1;i<=a;i+=1){
            c=c*i;
        }
        cout <<findv(index,c);
        cout << endl;
    }

}

改了也不行啊


by Kete @ 2024-07-18 12:55:11

@Chen_Three

这是代码上的问题,还有逻辑上的问题


by Kete @ 2024-07-18 12:55:56

我现在做这道题,帮你分析一下


by Kete @ 2024-07-18 13:13:19

@Chen_Three

我写了一个代码可以AC,代码如下:

#include<bits/stdc++.h>
using namespace std;
int s[11001],t,u; 
void cf(int u) { //用来进行乘法的函数
    for(int i=1; i<=t; i++) { //首先,先从1到u进行循环,让s数组的每一位都乘上一个u
        s[i]*=u;//将s数组的第i位乘以u
    }
    for(int i=1; i<=t; i++) { //即循环s数组的每一位
        if(s[i]>9) { //如果s数组的第i为需要进行进位操作
            s[i+1]+=s[i]/10;//根据满十进一
            s[i]%=10;//进完位之后剩下的个位 
            if(i+1>t) { //如果当前所在的第i位是最高位
                t++;//将遍历范围扩大一 
            }
        }
    }
    return;//结束此函数
}
int main() {
    int n=0;//n代表的是接下来将会有n组数据 
    scanf("%d",&n);//读入n
    while(n--) { //做n次循环
        int ans=0,l=0;//初始化变量
        memset(s,0,sizeof(s));//将s数组的每一位都刷成0
        s[1]=1;//第一位一定要是1,因为0乘以任何数的结果都为0,所以s数组的第一位要是1
        t=1;//一开始这个数只有一位 如上 
        scanf("%d %d",&u,&l);//读入 u!里面有多少个l这个数字
        for(int i=1; i<=u; i++) { //开始求u的阶乘
            cf(i);//将s数组乘以i
        }
        for(int i=t; i>=1; i--) { //开始找u!里面有多少个l这个数字
            if(s[i]==l) { //如果找到了一个
                ans++;//那么就将答案数加1
            }
        }
        printf("%d\n",ans);//输出答案
    }
    return 0;//结束程序
}

为了方便理解,所以注释有点多-----AC提交证明


by Chen_Three @ 2024-07-18 14:49:42

@shengzhe0227 感觉咱俩的差不多,为什么我WA


by Chen_Three @ 2024-07-18 14:53:25

@shengzhe0227

#include<bits/stdc++.h>
using namespace std;

struct bigNumber{
    int len , x[N] ;
    bigNumber( ) {len = 1 ; memset(x, 0 , sizeof(x)) ;}
    void print() {
        for (int i = len ;i;i--) printf("%d",x[i]) ;cout << endl;
    }
} a,b,c;

bigNumber fix(bigNumber a ) {
    for (int i = 1;i< a.len;++ i) {
        if (a.x[i] <0) a.x[i]+= 10 , a.x[i+1] -- ;
        a.x[i+1] += a.x[i] / 10 ;
        a.x[i] %= 10 ;
    }
    while (a.x[a.len] >=10 ) {
        a.x[a.len + 1] += a.x[a.len] / 10 ;
        a.x[a.len] %= 10 ; a.len ++ ;
    }
    while (!a.x[a.len] && a.len>1) a.len-- ;
    return a ;  
}

inline bigNumber operator *(bigNumber a , int b) {
    for (int i = 1;i<= a.len ;++ i ) a.x[i] *= b ;
    return fix(a) ;
}

int findv(int ind,bigNumber a){
    int cnt = 0;
    for(int i = 1;i <= a.len; i++){
        if(a.x[i]==ind){
            cnt=cnt+1;
        }
    }
    return cnt;
}
int main(){
    bigNumber b,c;
    int T,index,a;cin >> T;
    while(T--){
        c.x[1]=1;
        cin >> a;
        cin >> index;
        for(int i=1;i<=a;i+=1){
            c=c*i;
        }
        cout <<findv(index,c);
        cout << endl;
    }

}

把没用的删了


by Chen_Three @ 2024-07-18 14:57:17

@shengzhe0227 已过,c忘记清零了


by Chen_Three @ 2024-07-18 14:57:55

已过,此贴结


by Kete @ 2024-07-19 09:15:58

抱歉啊,我看的时候只是大概看了一下

已关注,求回关


| 下一页