zoukankan      html  css  js  c++  java
  • 随笔—邀请赛前练— Duff in Love

    题意:给一个数,求最大的一个因子,这个因子还要满足不能有平方数是他的因子。

    我的解法几乎是暴力的,应该可以用数学的方法不暴力(或者说不那么“暴力”)求出来。

    我的解法是:

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<cmath>
    #include<set>
    
    using namespace std;
    
    #define  MAX(x,y) (((x)>(y)) ? (x) : (y))
    #define  MIN(x,y) (((x) < (y)) ? (x) : (y))
    #define ABS(x) ((x)>0?(x):-(x))
    
    const long long inf = 0x7fffffff;
    const long long N=1000000+10;
    long long a[N];
    
    bool check(long long x)
    {
        for(long long i=2; i*i<=x; i++){
            if(x%(i*i) ==0){
                return 0;
            }
        }
        return 1;
    }
    
    int main()
    {
        long long n;
        cin>>n;
        long long p=0;
        for(long long i=1; i*i<=n; i++){
    //        cout<<i<<endl;
            if(n%i == 0){
                    a[p++]=i;
                    a[p++]=n/i;
            }
        }
        sort(a,a+p);
        for(long long i=p-1; i>=0; i--){
            if( check(a[i])){
                printf("%I64d
    ",a[i]);
                break;
            }
        }
    View Code

    后来在网上学习了质因子分解的解法。  思路是从2开始,一个个试因子,并且把n中的i因子抽离干,进行到n==1或者i*i>n 。

    代码

    #include<iostream>
    using namespace std;
    
    int main()
    {
        long long n;
        cin>>n;
        long long ans=1;
        for(long long i=2; i*i<=n; i++){
            if(n==1)    break;
            if(n%i == 0)    
                ans *= i;
            while(n%i == 0){
                n /= i;
            }
        }
        cout<<ans*n<<endl;
        return 0;
    }
    View Code
  • 相关阅读:
    Easy Climb UVA
    POJ 2823 滑动窗口 单调队列模板
    Feel Good
    Problem J. Joseph’s Problem 约瑟夫问题--余数之和
    hdu 1029 Ignatius and the Princess IV
    poj 1027 Ignatius and the Princess II全排列
    Problem C Updating a Dictionary
    hdu 1412 {A}+{B}
    hdu 4006 The kth great number
    实现:职工管理系统
  • 原文地址:https://www.cnblogs.com/shawn-ji/p/5538133.html
Copyright © 2011-2022 走看看