HDU4675【GCD of scequence】【组合数学、费马小定理、取模】
来源:互联网 发布:网页设计需要美工吗 编辑:程序博客网 时间:2024/05/22 14:50
看题解一开始还有地方不理解,果然是我的组合数学思维比较差
然后理解了之后自己敲了一个果断TLE。。。。
我以后果然还得多练啊
好巧妙的思路啊
知识1:
费马小定理是数论中的一个重要定理,其内容为:假如p是质数,且(a,p)=1,那么 a^(p-1) ≡1(mod p)假如p是质数,且a,p互质,那么 a的(p-1)次方除以p的余数恒等于1
对于除法取模还需要用到费马小定理: a ^ (p - 1) % p = 1; -> a ^ (p - 2) % p = (1 / a) % p;
巧妙1:
for(int i=1;i<=n;i++)
{ int temp; scanf("%d",&temp); sum1[temp]++; }
for(int j=i;j<=m;j+=i) sum+=sum1[j];
直接判断倍数是否有无。ORZ!!!
用这一块代码,这样再从1遍历到m的时候,速度增加非常快,然后就不会超时。
#include <cstdio>#include <cstdlib>#include <cstring>#include <cstring>#include <cmath>#include <ctime>#include <iostream>#include <algorithm>#include <string>#include <queue>#include <set>#include <map>#include <vector>#include <assert.h>using namespace std;#define lowbit(i) (i&-i)#define sqr(x) ((x)*(x))#define enter printf("\n")#define is_sqr(x) (x&(x-1))#define pi acos(-1.0)#define clr(x) memset(x,0,sizeof(x))#define fp1 freopen("in.txt","r",stdin)#define fp2 freopen("out.txt","w",stdout)#define pb push_backtypedef __int64 LL;const double eps = 1e-7;const double DINF = 1e100;const int INF = 1000000006;const LL LINF = 1000000000000000005ll;const int MOD = (int) 1e9 + 7;const int maxn=300005; template<class T> inline T Min(T a,T b){return a<b?a:b;} template<class T> inline T Max(T a,T b){return a>b?a:b;} template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);} template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}LL f[maxn],e[maxn],a[maxn],ans[maxn],sum1[maxn];LL quick_pow(LL a,LL b)//a的b次方,快速幂取模{ LL ret=1; while(b) { if(b&1) ret=(ret*a)%MOD; b/=2; a=(a*a)%MOD; } return ret%MOD;}LL cal(LL n,LL k){ if(k==0||n==k) return 1; return (f[n]*e[k]%MOD)*e[n-k]%MOD;//注意运算顺序}//以后某些变量还是不采用C99的写法了int main(){ f[0]=e[0]=1; for(int i=1;i<=maxn;i++) { f[i]=f[i-1]*i%MOD; e[i]=quick_pow(f[i],MOD-2); } int n,m,k; while(scanf("%d%d%d",&n,&m,&k)!=EOF) { clr(sum1); for(int i=1;i<=n;i++) { int temp; scanf("%d",&temp); sum1[temp]++; } for(int i=m;i>=1;i--)//倒着写不至于每次都m/i次循环 { int sum=0; for(int j=i;j<=m;j+=i) sum+=sum1[j]; if(sum<n-k)//k个不一样的,n-k个一样的。 { ans[i]=0;continue; } ans[i]=((cal(sum,n-k)*quick_pow(m/i-1,sum-(n-k))%MOD)*quick_pow(m/i,n-sum))%MOD; for(int j=2*i;j<=m;j+=i) ans[i]=(ans[i]-ans[j]+MOD)%MOD; } for(int i=1;i<m;i++) printf("%lld ",ans[i]); printf("%lld\n",ans[m]); } return 0;}
- HDU4675【GCD of scequence】【组合数学、费马小定理、取模】
- HDU4675-GCD of Sequence(数论+组合计数)
- HDU4675 GCD of Sequence
- hdu4675——GCD of Sequence(莫比乌斯反演+组合数取模+乘法逆元+快速幂)
- HDU4675 GCD of Sequence(预处理阶乘逆元+推公式)
- hdu 5651 组合数学+费马小定理求逆元
- Codeforces554C组合数学+费马小定理求逆元
- 组合取模问题与Lucas定理
- c语言 组合数学+大数取模
- HDU 4675GCD of Sequence(大组合数取模 容斥原理 组合数学)
- 组合数学各种小定理
- 多校第一场 费马小定理+模拟+组合数学
- Codeforces554C:Kyoya and Colored Balls(组合数学+费马小定理)
- CodeForces - 615D Multipliers 费马小定理+组合数学
- HDU4869 组合数学+费马小定理求逆元+快速幂
- HDU 4704 Sum(费马小定理,组合数学,快速幂)
- 组合数对素数取模 Lucas定理
- Lucas定理(大组合数的取模)
- HDFS中的通信协议
- 变量声明和定义的区别
- 微信公众平台开发[8]-开发模式-高级功能-获取关注用户的信息
- 监听Activity进入后台(最小化),并根据时间判断是否超时,此解决办法可用于超时重登陆
- bean的作用域
- HDU4675【GCD of scequence】【组合数学、费马小定理、取模】
- hdu3336(KMP)
- HDU 4308 Saving Princess claire_
- 【转】当ping遭遇问号
- 字符设备驱动
- PLSQL中文显示乱码
- Mahout源码解读_ToItemPrefsMapper
- E2072 Number of elements (11) differs from declaration (15)
- CodeForces 144A Arrival of the General