2017-03-04 set—难解的集合
来源:互联网 发布:a byte of python csdn 编辑:程序博客网 时间:2024/04/29 10:59
Description
定义集合 A={1,2,3…,n} , 现在有一个 A->A 的函数 f(x) , 也就是说这个函数的 定义域是 A, 值域是 A 的非空子集。
现在请你找出有多少种可能的 f(x) , 使得对于所有 x0(x0∈ A) , f(f(x0)) 都为同 一个值。 请将方案数 mod 10^9+7 后输出 。
Input
第一行一个正整数 n, 表示集合 A。
Output
第一行一个数 Ans, 表示 f(x) 可能方案数。
Sample Input
3
Sample Output
9
样例解释
所有满足条件的 f() 如下(三个数分别表示 f(1) 、 f(2) 、 f(3) ):
{1 1 1} {1 1 2} {1 3 1}
{2 2 2} {2 2 1 } {2 3 3}
{3 3 3} {3 1 3} {3 2 2}
一共有 9 种不同的 f() 。
数据范围
对于 20%的数据, n≤8。
对于 40%的数据, n≤20。
对于 60%的数据, n≤1500。
对于 100%的数据, n≤10^5。
solution
组合数,乘法逆元,费马小定理,快速幂…..这些还是理理清楚比较好
用快速幂实现费马小定理,用费马小定理求逆元,用逆元求组合数,用组合数求在n-1个数中取L-1个数的方案数,用快速幂求剩余n-L个数在L-1个数中取的方案数,两者乘起来加上ans,最后ans*n,因为p有n种可能。
设最后都收束为p,显然f(p)=p。设有L个f(x)=p,除了p还要任选L-1个数,剩下n-L都要在这L-1个位置中任取。枚举L,复杂度为O(n);
其实最暴力的n的n次方的打表还是有20分的呢
Code
#include <cstdio>#include <algorithm>using namespace std;typedef long long int64;const int S = 100010;const int mo = (int)1e9 + 7;int n, fac[S];int c1, c2, ans;int ksm(int a, int b)//快速幂求a的b次方并返回逆元值{ if (a < 0 || b < 0) return 0; int w = 1; for (; b; b >>= 1, a = (int64)a * a % mo) if (b & 1) w = (int64)w * a % mo; return w;}/* 这个是错误的快速幂int ksm(int a,int b) { if(a<0||b<0) return 0; int m=1; b>>=1; while(b) { if(b&1) m=(int64)m*a%mo; a=a*a%mo; b>>=1; } return m; }*/int inv(int x)//用费马小定理求逆元 { return ksm(x,mo-2); }int main(){ freopen("set.in" , "r", stdin); freopen("set.out", "w", stdout); scanf("%d",&n); fac[0]=1; for(int i=1;i<=n;i++) fac[i]=(int64)fac[i-1]*i%mo;//求阶乘 for(int L=1;L<=n;L++) { c1=(int64)fac[n-1]*inv(fac[n-L])%mo*inv(fac[L-1])%mo;//在n-1中选取n-L个数的方案数 c2=(int64)ksm(n-L,L-1);//快速幂求n-L的L-1次方,即剩余的n-L个数有多少种选择方法 ans+=(int64)c1*c2%mo;ans%=mo; } ans=(int64)ans*n%mo;//总共第一个数可以取n种 printf("%d\n",ans); return 0;}
1 0
- 2017-03-04 set—难解的集合
- 房地产——特殊的产业,难解的迷局
- set集合的排序
- Python的集合set
- Set集合的实现
- Set集合的遍历
- Python的set集合
- set集合的遍历
- Set集合的练习
- 集合——Set集合
- Java集合—Set集合
- 难解的三元二次方程组。
- Vijos P1369难解的问题
- Vijos P1369难解的问题
- VIJOS-P1369 难解的问题
- 集合框架—Set
- Java_集合—Set
- 黑马程序员-集合框架的Set集合
- eclipse 开发遇见的问题1
- 疯狂的采药(完全背包)
- 组件的ID、事件监听器、 按钮点击效果
- ES6——Day1(块级作用域)
- 最佳浏览路线
- 2017-03-04 set—难解的集合
- 爬取今日头条图片
- CTF-实验吧-雌黄出其唇吻
- 打开jsp使用新窗口
- 第二次实验(作业博客)
- html登陆按钮和注册按钮
- 洛谷 P1092 虫食算
- 计算地球上任意两点(经纬度)距离
- rsyslog+mariadb+loganalyzer实现日志服务器搭建