题目如下:

Return the number of permutations of 1 to n so that prime numbers are at prime indices (1-indexed.)

(Recall that an integer is prime if and only if it is greater than 1, and cannot be written as a product of two positive integers both smaller than it.)

Since the answer may be large, return the answer modulo 10^9 + 7.

Example 1:

Input: n = 5
Output: 12
Explanation: For example [1,2,5,4,3] is a valid permutation, but [5,2,3,4,1] is not because the prime number 5 is at index 1.

Example 2:

Input: n = 100
Output: 682289015

Constraints:

  • 1 <= n <= 100

解题思路:题目不难,对于给定一个正整数n,很容易可以求出在1~n这个区间有几个素数。假设素数有x个,x个素数占据x的位置,其排列方式的总和是x!;同理非素数有(n-x)个,那么这些非素数的排列方式就有(n-x)!个,两者的乘积即为最终的答案。

代码如下:

class Solution(object):
def numPrimeArrangements(self, n):
"""
:type n: int
:rtype: int
"""
prime = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]
import bisect
prime_count = bisect.bisect_right(prime,n)
no_prime_count = n - prime_count
def calc(num):
factorial = 1
for i in range(1, num + 1):
factorial = factorial * i
return factorial total = calc(prime_count) * calc(no_prime_count)
return total % (10**9 + 7)

【leetcode】1175. Prime Arrangements的更多相关文章

  1. 【LeetCode】762. Prime Number of Set Bits in Binary Representation 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历数字+质数判断 日期 题目地址:https:// ...

  2. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  3. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  4. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  5. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  6. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  7. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  8. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  9. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

随机推荐

  1. nginx排查502错误

    排查502错误1.查看/usr/local/nginx/conf/nginx.conf从而知道其错误日志在哪.重点查看其错误日志.2.如果是/tmp/dd.sock2017/05/01 18:48:3 ...

  2. fiddler之数据分析和查看(inspectors)-抓包

    在instpectors中主要是对请求和响应进行查看和分享,监听请求的响应内容.他有多个分页标签.界面分上下两部分,上面部分显示请求的相关信息:下面部分显示响应相关信息.简单说明常用的几个分页标签 一 ...

  3. Java程序员面试题集(151-180)

    Java面试题集(151-180) 摘要:这部分包含了Spring.Spring MVC以及Spring和其他框架整合以及测试相关的内容,除此之外还包含了大型网站技术架构相关面试内容. 151. Sp ...

  4. java jna 调用windows动态链接库

    import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.Platform; import com.sun.j ...

  5. 2.转发。基于itchat的微信消息同步机器人

    原文:https://www.jianshu.com/p/7aeadca0c9bd# 看到了该网址有基于itchat的微信消息同步机器人,转过来继续研究.以下是转过来的: 最近 全栈数据工程师养成攻略 ...

  6. 2d平台怪物逻辑

    2d来回巡逻 遇到坑会自动转向 可配置单次方向行走的时间,转向等待时间等 using System; using System.Collections; using System.Collection ...

  7. Hand on Machine Learning第三章课后作业(2):其余小练习

    -#!/usr/bin/env python -# # # -- coding: utf-8 -- -# # # @Time : 2019.5.22 14:09 -# # # @Author : An ...

  8. Vue-cli项目与element导航菜单控件的结合使用以及遇到的问题

    1.基本使用 第一种常用写法:导航菜单与 router-view 的配合使用 将所用的导航菜单数据编写成一个数组的形式,提高维护性: 在utils工具文件夹中建立utils.js文件: import ...

  9. xmake新增对Qt编译环境支持

    在最新的xmake v2.2.1版本中,新增了对Qt SDK环境的支持,我们完全可以脱离Qt Creater进行Qt应用程序的开发,甚至配合vscode/idea等编辑器+xmake插件(xmake- ...

  10. Spring Boot 2.2.0 正式发布,支持 JDK 13!

    Java技术栈 www.javastack.cn 优秀的Java技术公众号 推荐阅读: Spring Boot 2.2.0 正式发布了,可从 repo.spring.io 或是 Maven Centr ...