Sum of NYOJ-91-factorial

Source: Internet
Author: User

Http://acm.nyist.net/JudgeOnline/problem.php? PID = 91

 

Factorial sum time limit: 3000 MS | memory limit: 65535 kb difficulty: 3
Description

We will give you a non-negative integer n to determine whether N is the sum of certain numbers (these numbers cannot be reused, and are positive numbers), such as 9 = 1! + 2! + 3 !, If yes, yes is output; otherwise, no is output;

Input
The first row has an integer of 0 <m <100, indicating that there are M groups of test data;
Each group of test data has a positive integer n <1000000;
Output
If the conditions are met, yes is output; otherwise, no is output;
Sample Input
2 9 10
Sample output
Yes No

 

Solution: preprocessing is less than a factorial of. Then, each input has a number of N, which reduces the factorial from large to small, and finally determines whether it is equal to 0.

 

1 # include <stdio. h>
2 # include <math. h>
3
4 long a [100], T;
5
6 int main (){
7 int n, m, I, J;
8 A [0] = 1;
9 I = 0;
10 while (A [I] <1000000 ){
11 I ++;
12 A [I] = A [I-1] * I;
13}
14 scanf ("% d", & M );
15 while (M --){
16 scanf ("% d", & N );
17 For (j = I-1; j> 0; j --){
18 if (n> = A [J]) {
19 N-= A [J];
20}
21}
22 if (n = 0 ){
23 printf ("Yes \ n ");
24}
25 else {
26 printf ("NO \ n ");
27}
28}
29 return 0;

30}

Sum of NYOJ-91-factorial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.