【leetcode】1175. Prime Arrangements
题目如下:
Return the number of permutations of 1 to
nso 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: 682289015Constraints:
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的更多相关文章
- 【LeetCode】762. Prime Number of Set Bits in Binary Representation 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历数字+质数判断 日期 题目地址:https:// ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
随机推荐
- 分布式任务队列 Celery —— 详解工作流
目录 目录 前文列表 前言 任务签名 signature 偏函数 回调函数 Celery 工作流 group 任务组 chain 任务链 chord 复合任务 chunks 任务块 mapstarma ...
- spring中的增强类型
在spring中有两种增强方式:XML配置文件和注解配置.下面一次为大家讲解. 使用的是Aspectj第三方框架 纯POJO (在XML中配置节点) 使用@AspectJ,首先要保证所用的JDK 是5 ...
- 用户tokenId
tokenId表示为:用户登录到成功后,服务端分配给客户端的令牌号,同时下发tokenId的过期时间.下次用户直接持有tokenId,在其过期时间内均可跳过用户登录步骤,直接请求其他服务操作.如果to ...
- Django框架效率问题的解决方法和总…
由于项目的需要,学习了Django框架,Django框架的MTV很清晰,通过MTV能够很好地了解Django框架的内部机理.但是在使用过程中发现了一个严重的问题,就是当有大量IO(写数据库操作)的时候 ...
- 获取当前页面的title
#-*-coding:utf-8-*-from selenium import webdriverdriver = webdriver.Firefox()driver.get("https: ...
- 将python 2.6 升级到 2.7,及pip安装
由于CentOS6.5 自带python版本为2.6.6,实际中使用的大多为2.7.x版本.于是手动升级. 查看python的版本 #python -VPython 2.6.6 1.下载Python- ...
- RabbitMQ 安装步骤
RabbitMQ安装步骤 一.安装erlang 1.下载erlang wget https://packages.erlang-solutions.com/erlang-solutions-1.0-1 ...
- 取石子游戏 HDU 1527 博弈论 威佐夫博弈
取石子游戏 HDU 1527 博弈论 威佐夫博弈 题意 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两 ...
- Luogu P2168 [NOI2015]荷马史诗
题目 哈夫曼树的每个叶子结点都有一个权值(表示某数据的出现频率),且\(\sum dis_ival_i\)最小. 哈夫曼树中,权值和越大的集合离根节点越近. 而每个数据对应从根节点到该叶子结点的一种编 ...
- git学习指南
近来学习Git,苦寻资料下发现廖雪峰老师的教程很好,在此推荐传送门 附每节总结,方便查阅 创建版本库 初始化一个Git仓库,使用git init命令. 添加文件到Git仓库,分两步: 使用命令git ...