zoukankan      html  css  js  c++  java
  • projectEuler 12 Highly divisible triangular number

    题意

    求最小的因子个数大于(500)的三角形数

    分析

    可以发现三角形数(frac{n(n+1)}{2})​中的(n)​和(n+1)​是互质的。不妨设(n)​为偶数,那么(d(frac{n(n+1)}{2})=d(frac{n}{2})d(n+1))​​。

    又有(d(frac{9699690}{2}) imes d(9699689)=128 imes8=1024)​(其中(9699690=2 imes3 imes5 imes7 imes11 imes13 imes17 imes19)​​​),只要用线性筛筛({10}^7)以内的质数即可。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    using ll=long long;
    constexpr int N(1e7);
    int p[N],pcnt;
    int c[N],cp[N],d[N];
    
    int main() {
      for(int i=2;i<N;i++) {
        if(!p[i]) {
          p[++pcnt]=i;
          c[i]=1;
          cp[i]=i;
          d[i]=2;
        }
        for(int j=1;j<=pcnt && p[j]<N/i;j++) {
          p[i*p[j]]=1;
          if(i%p[j]!=0) {
            c[i*p[j]]=1;
            cp[i*p[j]]=p[j];
            d[i*p[j]]=d[i]*2;
          } else {
            c[i*p[j]]=c[i]+1;
            cp[i*p[j]]=cp[i]*p[j];
            d[i*p[j]]=d[i/cp[i]]*(c[i*p[j]]+1);
            break;
          }
        }
        int x=i,y=i-1;
        if(x%2) swap(x,y);
        int ans=d[x]/(c[x]+1)*c[x]*d[y];
        if(ans>500) {
          cout<<i-1<<','<<i<<endl;
          cout<<(1ll*x*y/2)<<endl;
          break;
        }
      }
    
      return 0;
    }
    
    
  • 相关阅读:
    [POJ]poj2632(模拟)
    [EOJ]2019 ECNU XCPC March Selection #2
    [POJ]POJ1328(trie)
    卡特兰数相关总结
    2019海亮夏令营随笔
    树上数数 题解
    护卫小队 题解
    洛谷 P2966 [USACO09DEC]牛收费路径Cow Toll Paths 题解
    洛谷 P4735 最大异或和
    登峰造极 题解
  • 原文地址:https://www.cnblogs.com/intmian/p/15139202.html
Copyright © 2011-2022 走看看