码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Factorial Divisibility(数论,1600)


    You are given an integer xx and an array of integers a1,a2,…,anYou have to determine if the number a1!+a2!+…+an! is divisible by x!.

    Here k!k! is a factorial of k — the product of all positive integers less than or equal to kk. For example, 3!=1⋅2⋅3=63!=1⋅2⋅3=6, and 5!=1⋅2⋅3⋅4⋅5=120.

    Input

    The first line contains two integers n and x (1≤n≤500000, ).

    The second line contains n integers a1,a2,…,an (1≤ai≤x) — elements of given array.

    Output

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

    思路:

    一开始一直想将阶乘的和转化为乘积的形式求解,未果。

    正解:

    先开桶记录下1~x-1的数量cnt[k]。不难发现当cnt[k]>=k+1时,可以做以下两个操作

    cnt[k]-=k+1;cnt[k+1]+=1; 

    k!*(k+1)=(k+1)!

    直至cnt[k].(1=

    此时对于1= (1<=k

    max\sum cnt[k]*k!<=\sum k!*k=\sum (k+1)!-k!=(2!-1!)+(3!-2!)+.....+(x!-(x-1)!)=x!-1

    因此只有当cnt[k] (1=

    代码:

    1. #include
    2. using namespace std;
    3. typedef long long ll;
    4. typedef pair<int,int> PII;
    5. const int N=5e5+10;
    6. int a[N],t[N];
    7. int main()
    8. {
    9. ios::sync_with_stdio(false);
    10. cin.tie(nullptr);
    11. cout.tie(nullptr);
    12. int n,x;
    13. cin>>n>>x;
    14. for(int i=1;i<=n;i++){
    15. cin>>a[i];
    16. t[a[i]]++;
    17. }
    18. for(int i=1;i
    19. if(t[i]>i){
    20. t[i+1]+=t[i]/(i+1);
    21. t[i]%=i+1;
    22. }
    23. if(t[i]) {cout<<"NO\n";return 0;}
    24. }
    25. cout<<"YES\n";
    26. return 0;
    27. }

     总结:

    对于此题可以得出一个结论

    \sum k*k!=x!-1 (1

    也就是说对于正整数x,所有小与x的数k的阶乘和,当每个cnt[k]<=k时,和不会超过x!

  • 相关阅读:
    数一独有:多元函数积分学中的基本公式及其应用
    数据结构——栈和队列
    pointpillars训练的输出信息
    封神榜系列模型基础环境(fengshenbang-LM-main),包括(deepspeed、jieba_fast)
    linux12企业实战 -- 56 zabbix常规监控错误汇总
    ScheduledExecutorService详解
    【力扣刷题笔记第二期】Python 数据结构与算法
    集群路径规划学习(一)之EGO-swarm仿真
    Java基础面试-多线程
    Web学习笔记-React(组合Components)
  • 原文地址:https://blog.csdn.net/m0_63761643/article/details/127760804
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号