• Factorial Divisibility(多个数的阶乘之后是否整除另一个数的阶乘)


    题意:

    给定n个数a1,a2,…,an和数x
    问a1!+a2!+…+an!是否可以被x!整除

    Input

    The first line contains two integers nn and xx (1 \le n \le 500\,0001≤n≤500000, 1 \le x \le 500\,0001≤x≤500000).

    The second line contains nn integers a_1, a_2, \ldots, a_na1​,a2​,…,an​ (1 \le a_i \le x1≤ai​≤x) — elements of given array.

    Output

    In the only line print "Yes" (without quotes) if a_1! + a_2! + \ldots + a_n!a1​!+a2​!+…+an​! is divisible by x!x!, and "No" (without quotes) otherwise.

    Sample 1

    InputcopyOutputcopy
    6 4
    3 2 2 2 3 3
    
    Yes
    

    Sample 2

    InputcopyOutputcopy
    8 3
    3 2 2 2 2 2 1 1
    
    Yes
    

    Sample 3

    InputcopyOutputcopy
    7 8
    7 7 7 7 7 7 7
    
    No
    

    Sample 4

    InputcopyOutputcopy
    10 5
    4 3 2 1 4 3 2 4 3 4
    
    No
    

    Sample 5

    InputcopyOutputcopy
    2 500000
    499999 499999
    
    No
    

    题解:

    1.ct[]表示小于x的a的阶乘和,用ct[] 数组记录每个给出的小于x的数的出现次数(大于等于x的一定可以整除),即 ct[a_{i}]++

    2.当ct[a]>a+1时,即a出现的次数大于 a+1 时,进行操作:ct[a]=ct[a]-a-1;ct[a+1]=ct[a+1]+1;

    解释:因为下标代表的是a的阶层,则 a!*(a+1)=(a+1)!.所以操作后,这n个数的总和不变。

    3.如果这些小于x的数的阶乘之和可以整除x的话,那么它们的可可以表示=k*x!。k为大于0的任意整数。表现在ct数组上,即为当i

    1. #include
    2. #include
    3. using namespace std;
    4. const long long N = 5e5 + 100;
    5. int ct[N];
    6. int main()
    7. {
    8. int n, x,v;
    9. cin >> n >> x;
    10. for (int i = 1; i <= n; i++)
    11. {
    12. cin >> v;
    13. if(v
    14. ct[v]++;
    15. }
    16. int f = 1;
    17. for (int i = 1; i < x; i++)
    18. {
    19. if (ct[i] >= i + 1)
    20. {
    21. ct[i + 1] = ct[i + 1] + ct[i]/(i+1);
    22. ct[i] = ct[i] - ct[i] / (i + 1) * (i + 1);
    23. }
    24. if (ct[i] != 0)
    25. {
    26. f = 0;
    27. cout << "No" << endl;
    28. break;
    29. }
    30. }
    31. if (f)
    32. cout << "Yes" << endl;
    33. }

  • 相关阅读:
    《从0开始写一个微内核操作系统》6-GIC中断
    MySQL之索引
    极验--一键通过模式逆向分析
    linux ipmitool - 硬件管理软件
    九、T100月加权成本次要素分析篇
    312.戳气球
    Mybatis01
    网络安全(黑客)自学
    SpringCloud Alibaba第1章
    vue3 搭配ElementPlus做基础表单校验 自定义表单校验
  • 原文地址:https://blog.csdn.net/weixin_62599885/article/details/127578146