• LightOJ 1236 Pairs Forming LCM 合数分解


    题意:求所有小于等于n的,x,y&&lcm(x,y)==n的个数

    分析:因为n是最小公倍数,所以x,y都是n的因子,而且满足这样的因子必须保证互质,由于n=1e14,所以最多大概在2^13个因子 即8000多因子

            所以每次可以递归暴力寻找一个因子,然后选好了以后,看唯一分解不同种素数还有哪种没有用,符合条件的只能用这些没有用过的,然后直接统计

    注:由于最终每个对都被统计了两次,所以/2,由于本身也算一对,所以+1

    代码:

    #include <cstdio>
    #include <iostream>
    #include <ctime>
    #include <vector>
    #include <cmath>
    #include <map>
    #include <queue>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    typedef long long LL;
    const int N=1e7+5;
    const int INF=0x3f3f3f3f;
    int cnt;
    bool v[N];
    LL prime[700000];
    void getprime(){
      for(int i=2;i*i<=N-5;++i)
        if(!v[i])
          for(int j=i*i;j<=N-5;j+=i)
            v[j]=1;
      for(int i=2;i<=N-5;++i)
      if(!v[i])prime[++cnt]=i;
    }
    int ans;
    vector<LL>g,c;
    bool vis[100];
    void dfs(int pos,LL res){
      if(pos==g.size()){
        int tmp=1;
        for(int i=0;i<g.size();++i){
          if(vis[i])continue;
          tmp*=(c[i]+1);
        }
        ans+=tmp;
        return;
      }
      dfs(pos+1,res);
      vis[pos]=1;
      for(LL i=1,k=g[pos];i<=c[pos];++i,k*=g[pos])
        dfs(pos+1,res*k);
      vis[pos]=0;
      return;
    }
    int main()
    {
        getprime();
        int cas=0,T;
        scanf("%d",&T);
        while(T--){
          LL t,n;
          scanf("%lld",&n),t=n;
          g.clear(),c.clear();
          for(int i=1;i<=cnt&&prime[i]*prime[i]<=t;++i){
              if(t%prime[i])continue;
              int tot=0;
              g.push_back(prime[i]);
              while(t%prime[i]==0)t/=prime[i],++tot;
              c.push_back(tot);
          }
          if(t>1)g.push_back(t),c.push_back(1);
          ans=0;
          dfs(0,1);
          printf("Case %d: %d
    ",++cas,(ans>>1)+1);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    小程序开发系列(五)悬浮搜索框
    LINQ的连接扩展(左连、右连、全连等)
    小程序开发系列(四)九宫格另一种实现
    python 生成随机图片验证码
    django定时任务小插件
    线程池模块thernd
    python logging 模块记录日志
    django Q条件
    jquery 事件绑定
    jQuery示例
  • 原文地址:https://www.cnblogs.com/shuguangzw/p/5384803.html
Copyright © 2020-2023  润新知