結果

問題 No.500 階乗電卓
ユーザー 101000010
提出日時 2017-05-17 03:23:24
言語 Java
(openjdk 23)
結果
AC  
実行時間 138 ms / 2,000 ms
コード長 800 bytes
コンパイル時間 3,454 ms
コンパイル使用メモリ 77,772 KB
実行使用メモリ 41,588 KB
最終ジャッジ日時 2024-06-25 03:01:38
合計ジャッジ時間 7,528 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class No500 {
    public static void main(String[] args){
        long N=0;
        Scanner sc = new Scanner(System.in);
        N = sc.nextLong();
        
        culc(N);
    }
    static void culc(long N){
        long mod = (long) Math.pow(10, 12); //1,000,000,000,000
        boolean moreThan12Digits = false;
        long ans=1;
        
        for(int i=2; i<=N; i++){    
            ans *= i;
            if(moreThan12Digits && ans == 0)break;  //下12桁が全て0になったら終了
            if(ans >= mod){
                ans %= mod;
                moreThan12Digits = true;
            }
        }
        if(moreThan12Digits){
            System.out.printf("%012d\n", ans);
        }else{
            System.out.println(ans);
        }
    }
}
0