看题解一开始还有地方不理解,果然是我的组合数学思维比较差

然后理解了之后自己敲了一个果断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_back typedef __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】【组合数学、费马小定理、取模】的更多相关文章

  1. hdu 4704 Sum【组合数学/费马小定理/大数取模】By cellur925

    首先,我们珂以抽象出S函数的模型:把n拆成k个正整数,有多少种方案? 答案是C(n-1,k-1). 然后发现我们要求的是一段连续的函数值,仔细思考,并根据组合数的性质,我们珂以发现实际上答案就是在让求 ...

  2. Codeforces554C:Kyoya and Colored Balls(组合数学+费马小定理)

    Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are ...

  3. 第十四届华中科技大学程序设计竞赛 B Beautiful Trees Cutting【组合数学/费马小定理求逆元/快速幂】

    链接:https://www.nowcoder.com/acm/contest/106/B 来源:牛客网 题目描述 It's universally acknowledged that there'r ...

  4. hdu 4704(费马小定理+快速幂取模)

    Sum                                                                                Time Limit: 2000/ ...

  5. poj 3734 Blocks 快速幂+费马小定理+组合数学

    题目链接 题意:有一排砖,可以染红蓝绿黄四种不同的颜色,要求红和绿两种颜色砖的个数都是偶数,问一共有多少种方案,结果对10007取余. 题解:刚看这道题第一感觉是组合数学,正向推了一会还没等推出来队友 ...

  6. hdu 4704 Sum (整数和分解+快速幂+费马小定理降幂)

    题意: 给n(1<n<),求(s1+s2+s3+...+sn)mod(1e9+7).其中si表示n由i个数相加而成的种数,如n=4,则s1=1,s2=3.                  ...

  7. 数论初步(费马小定理) - Happy 2004

    Description Consider a positive integer X,and let S be the sum of all positive integer divisors of 2 ...

  8. HDU 5793 A Boring Question (逆元+快速幂+费马小定理) ---2016杭电多校联合第六场

    A Boring Question Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  9. [HDOJ5667]Sequence(矩阵快速幂,费马小定理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5667 费马小定理: 假如p是质数,且gcd(a,p)=1,那么 a^(p-1)≡1(mod p). 即 ...

随机推荐

  1. 17、android设备如何防止屏幕休眠(转载)

    当你需要你的设备需要长期运行时,由于移动设备为了延长电池续航时间,在运行15s-30mins后(用户可自由设置),如果用户在此时间段内没有操作,系统将进入休眠状态并 将屏幕锁上,所以在需要长期运行时, ...

  2. EntityFramework走马观花之CRUD(上)

    对于任何一个ORM框架,CRUD都是其核心功能,可以这么说,CRUD功能实现得好坏,直接决定了此ORM框架的命运. CRUD是英文Create.Read.Update.Delete四个单词的缩写,对应 ...

  3. TCL随记(1)

    string 函数: string compare [-nocase] [-length int] str1 str2 把字符串str1和str2进行比较,返回值为-1/0/1,分别对应str1小于/ ...

  4. 防止IE7,8进入怪异模式

    在页头添加 <meta http-equiv="X-UA-Compatible" content="IE=edge" />

  5. cookie和session的代码实现

    cookie和session的代码实现 1.设置cookie 今天笔试题考的是cookie的设置,我竟然选了request也可以设置cookie,我的天呀. 我们来看如何在response设置吧 pu ...

  6. AutoResetEvent 运用

    static AutoResetEvent are = new AutoResetEvent(true);//初始化为开 static void Main(string[] args) { //如果这 ...

  7. 四大机器学习降维算法:PCA、LDA、LLE、Laplacian Eigenmaps

    四大机器学习降维算法:PCA.LDA.LLE.Laplacian Eigenmaps 机器学习领域中所谓的降维就是指采用某种映射方法,将原高维空间中的数据点映射到低维度的空间中.降维的本质是学习一个映 ...

  8. windows phone中ListBox的简单使用

    学习windows phone数据绑定的一点点心得,在wp系统的APP中经常遇到这样风格的软件,那它们到底怎样实现的呢?我就大致去做了一下,比较粗虐,但基本的都已经有了,实现后的结果为: 哇,这个图截 ...

  9. JAVA敏捷开发环境搭建(转载)

    前面介绍了创业型软件公司的工作模式,这里详细介绍下如何实施,第一步是先要搭建环境,有了环境才能开展工作. 整个软件项目分为四个环境 开发本地环境.开发环境.测试环境.IDC环境.和传统C++开发不一样 ...

  10. Sqli-labs less 40

    Less-40 本关的sql语句为SELECT * FROM users WHERE id=('$id') LIMIT 0,1 我们根据sql语句构造以下的payload: http://127.0. ...