29ms,最憋屈的超时!
查看原帖
29ms,最憋屈的超时!
1074084
asd890123楼主2024/5/20 17:24

我知到正解时递推,但是我发现exgcd的表现如下: printf输出,用时最长591ms; std::cout + 关闭同步 + '\n',用时最长544ms; fwrite快写输出,用时最长529ms,所以求卡常?! 说一句,我知道700ms才会停止计时,所以这是准的,fwrite快写代码如下:

#include <cstdio>
#include <tuple>

namespace QuikIO{
    
    int p3;
    const int size = 1 << 14;
    char out[size];
    
    #define flush() (fwrite(out,1,p3,stdout),p3 = 0)
    #define putchar(ch) (p3 == size && flush(),out[p3++] = ch)
    class Flush{public:~Flush(){flush();}}_;
    
    void write(int x){
        
        if (x > 9) write(x / 10);
        putchar(x % 10 + '0');
        
    }
    
    void print(int x,char end = '\n'){
        
        write(x);
        putchar(end);
        
    }
    
}using QuikIO::print;

std::tuple <int,int,int> exgcd(int a,int b){
    
    if (b == 0) return {1,0,a};
    auto [x0,y0,gcd] = exgcd(b,a % b);
    
    return {y0,x0 - a / b * y0,gcd};
}

#define inv(a,p) ((std::get<0>(exgcd(a,p)) % (p) + (p)) % p)

int main(){
    
    int n,p;
    
    scanf("%d%d",&n,&p);
    for (int i = 1;i <= n;i++) print(inv(i,p));
    
    return 0;
}
2024/5/20 17:24
加载中...