设为首页 加入收藏

TOP

HDU4675[GCD of scequence][组合数学、费马小定理、取模]
2014-11-23 19:05:22 来源: 作者: 【 】 浏览:6
Tags:HDU4675 GCD scequence 组合 数学 费马小 定理 取模

知识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    
#include    
#include    
#include    
#include    
#include    
#include    
#include    
#include    
#include    
#include    
#include    
#include    
#include    
  
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 inline T Min(T a,T b){return a inline T Max(T a,T b){return a>b a:b;}  
 template inline T Min(T a,T b,T c){return min(min(a, b),c);}  
 template 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
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

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 inline T Min(T a,T b){return a inline T Max(T a,T b){return a>b a:b;}
 template inline T Min(T a,T b,T c){return min(min(a, b),c);}
 template 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 
 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU4628+状态压缩DP 下一篇hdu 4565 So Easy!(矩阵+快速幂..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Python 教程 - W3Sch (2025-12-26 12:00:51)
·Python基础教程,Pyt (2025-12-26 12:00:48)
·神仙级python入门教 (2025-12-26 12:00:46)
·“我用Java 8”已成 (2025-12-26 11:19:54)
·下载 IntelliJ IDEA (2025-12-26 11:19:52)