Trang

Chủ Nhật, 2 tháng 1, 2011

1150. Sum of Factorials

import java.io.*;

public class Main {

    /**
     * http://acm.tju.edu.cn/toj/showp1150.html
     * @author hunglee
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(
                new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(System.out);
        
        int n, i;
        int[] f = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
        do {
            in.nextToken();
            n = (int) in.nval;
            if (n < 0) break;
            if (n == 0) {
                out.println("NO");
                continue;
            }
            
            i = 9;
            while (n > 0 && i >= 0) {
                while (i >= 0 && f[i] > n) --i;
                if (i >= 0)
                    n -= f[i];
                --i;
            }
            if (n == 0)
                out.println("YES");
            else
                out.println("NO");
        } while (true);
        out.flush();
    }

}

Không có nhận xét nào:

Đăng nhận xét