求助(高精总是错

P1591 阶乘数码

MC_dream_tsr @ 2024-08-31 13:47:48

全wa,不要AC代码和以P开头的语言,求改,谢谢


# include<bits/stdc++.h>
using namespace std;
const int kk = 3000;
int a1[kk], n[1005], a[1005];
void a1_0() {
    for(int i = 0; i < 3000; i++) a1[i] = 0;
}
int jc(int x) {
    int ans = 0, len = 1;
    for(int i = 2; i <= n[x]; i++)
        for(int j = 0; j < len; j++)
            a1[j] *= i;
    for(int i = 0; i < len; i++){
        a1[i + 1] += a1[i] / 10, a1[i] %= 10;
        if(a1[len]) len++;
    }
    for(int i = len - 1; i >= 0; i--)
        if(a1[i] == a[x]) ans++;
    a1_0();
    return ans;
}
int main(){
    int t;
    cin >> t;
    for(int i = 1; i <= t; i++) cin >> n[i] >> a[i];
    for(int i = 1; i <= t; i++) cout << jc(i) << endl;
    return 0;
}

by Yxy7952 @ 2024-08-31 13:55:41

@MC_dream_tsr

# include<bits/stdc++.h>
using namespace std;
const int kk = 3000005;
int a1[kk], n[1005], a[1005];
void a1_0() {
    for(int i = 0; i < 3000; i++) a1[i] = 0;
}
int jc(int x) {
    int ans = 0, len = 1;
    for(int i = 2; i <= n[x]; i++)//在这里就要爆int,方法错了,建议重写 
        for(int j = 0; j < len; j++)
            a1[j] *= i;
    for(int i = 0; i < len; i++){
        a1[i + 1] += a1[i] / 10, a1[i] %= 10;
        if(a1[len]) len++;
    }
    for(int i = len - 1; i >= 0; i--)
        if(a1[i] == a[x]) ans++;
    a1_0();
    return ans;
}
int main(){
    int t;
    cin >> t;
    for(int i = 1; i <= t; i++) cin >> n[i] >> a[i];
    for(int i = 1; i <= t; i++) cout << jc(i) << endl;
    return 0;
}

by MC_dream_tsr @ 2024-08-31 13:59:52

@yixingyou 什么方法错了,阶乘?int不会爆吧(大雾


by Yxy7952 @ 2024-08-31 14:03:16

@yixingyou @MC_dream_tsr

阶乘会爆

1000!


by MC_dream_tsr @ 2024-08-31 14:05:52

新改的,样例过了,可还是全wa


# include<bits/stdc++.h>
using namespace std;
const int kk = 3000;
int a1[kk] = {1}, n[1005], a[1005];
void a1_0() {
    for(int i = 0; i < 3000; i++) a1[i] = 0;
    a1[0] = {1};
}
int jc(int x) {
    int ans = 0, len = 1;
    for(int i = 2; i <= n[x]; i++)
        for(int j = 0; j < len; j++)
            a1[j] *= i;
    for(int i = 0; i < len; i++){
        a1[i + 1] += a1[i] / 10, a1[i] %= 10;
        if(a1[len]) len++;
    }
    for(int i = len - 1; i >= 0; i--)
        if(a1[i] == a[x]) ans++;
    a1_0();
    return ans;
}
int main(){
    int t;
    cin >> t;
    for(int i = 1; i <= t; i++) cin >> n[i] >> a[i];
    for(int i = 1; i <= t; i++) cout << jc(i) << endl;
    return 0;
}

by Yxy7952 @ 2024-08-31 14:08:39

@MC_dream_tsr

# include<bits/stdc++.h>
using namespace std;
const int kk = 3000;
int a1[kk] = {1}, n[1005], a[1005];
void a1_0() {
    for(int i = 0; i < 3000; i++) a1[i] = 0;
    a1[0] = {1};
}
int jc(int x) {
    int ans = 0, len = 1;
    for(int i = 2; i <= n[x]; i++)
        for(int j = 0; j < len; j++)
            a1[j] *= i;//当len=1时,a1[0]*=i; 如果n[x]很大,就会爆int 
    for(int i = 0; i < len; i++){
        a1[i + 1] += a1[i] / 10, a1[i] %= 10;
        if(a1[len]) len++;
    }
    for(int i = len - 1; i >= 0; i--)
        if(a1[i] == a[x]) ans++;
    a1_0();
    return ans;
}
int main(){
    int t;
    cin >> t;
    for(int i = 1; i <= t; i++) cin >> n[i] >> a[i];
    for(int i = 1; i <= t; i++) cout << jc(i) << endl;
    return 0;
}

by MC_dream_tsr @ 2024-08-31 14:09:49

@yixingyou 对啊,这是高精啊,那有数组


by MC_dream_tsr @ 2024-08-31 14:11:19

@yixingyou n≤1000


by Yxy7952 @ 2024-08-31 14:13:44

@MC_dream_tsr

你看这篇代码

# include<bits/stdc++.h>
using namespace std;
const int kk = 3000;
int a1[kk] = {1}, n[1005], a[1005];
void a1_0() {
    for(int i = 0; i < 3000; i++) a1[i] = 0;
    a1[0] = {1};
}
int jc(int x) {
    int ans = 0, len = 1;
    for(int i = 2; i <= n[x]; i++)
        for(int j = 0; j < len; j++)
            a1[j] *= i,cout<<a1[j]<<"\n";//当len=1时,a1[0]*=i; 如果n[x]很大,就会爆int 
    for(int i = 0; i < len; i++){
        a1[i + 1] += a1[i] / 10, a1[i] %= 10;
        if(a1[len]) len++;
    }
    for(int i = len - 1; i >= 0; i--)
        if(a1[i] == a[x]) ans++;
    a1_0();
    return ans;
}
int main(){
    int t;
    cin >> t;
    for(int i = 1; i <= t; i++) cin >> n[i] >> a[i];
    for(int i = 1; i <= t; i++) cout << jc(i) << endl;
    return 0;
}

在本地运行这个,看输出:

1
1000 1

by MC_dream_tsr @ 2024-08-31 14:19:07

输出了0,what???没理解咋爆的,普通高精阶乘不就是这样的吗?


by MC_dream_tsr @ 2024-08-31 14:19:16

@yixingyou


| 下一页