zoukankan      html  css  js  c++  java
  • 南阳OJ-91-阶乘之和---二进制枚举(入门)

    题目链接:
    http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=91

    题目大意:

    给你一个非负数整数n,判断n是不是一些数(这些数不允许重复使用,且为正数)的阶乘之和,如9=1!+2!+3!,如果是,则输出Yes,否则输出No;n<1000000;

    思路:

    数据量小,直接预处理出所有满足的数,然后直接判断就行了,预处理时用了二进制枚举子集的方式来处理

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<set>
     6 #include<cmath>
     7 using namespace std;
     8 const int maxn = 1e4 + 10;
     9 int T, n;
    10 int a[20];
    11 set<int>s;
    12 void judge(int x)
    13 {
    14     int tot = 0;
    15     for(int i = 0; i < 9; i++)
    16     {
    17         if(x&(1<<i))tot += a[i + 1];
    18     }
    19     s.insert(tot);
    20 }
    21 int main()
    22 {
    23     cin >> T;
    24     a[1] = 1;
    25     for(int i = 2; i <= 9; i++)a[i] = a[i - 1] * i;
    26     for(int i = 1; i < (1<<9); i++)
    27     {
    28         judge(i);
    29     }
    30     while(T--)
    31     {
    32         cin >> n;
    33         if(s.count(n))cout<<"Yes"<<endl;
    34         else cout<<"No"<<endl;
    35     }
    36     return 0;
    37 }
  • 相关阅读:
    python+selenium框架
    django--form组件
    python +selenium上传文件
    python--UI---登录---验证码
    python+selenium button定位方法
    css-定位技术
    css-盒子模型
    css-元素分类
    序列化
    FileUploadController
  • 原文地址:https://www.cnblogs.com/fzl194/p/8684101.html
Copyright © 2011-2022 走看看