zoukankan      html  css  js  c++  java
  • uva 10622

    http://vjudge.net/contest/140673#problem/H

    求某个数字(最大到10^9,可为负值)写成完全p次方数的指数p是多少

    分析: 先进行唯一分解,之后p整除各个素因子的指数,即求一组数的最大公约数.

     1                
     2                                                            //*  _________________________________________________________________________________
     3                                                               
     4                                                                 #include <iostream>
     5                                                                 #include <cstdio>
     6                                                                 #include <string> 
     7                                                                 #include <algorithm>
     8                                                                 #include <cmath>
     9                                                                 #include <vector>   
    10                                                                 #include <map>   
    11                                                                 using namespace std; 
    12                                                                 #define mem(A, X) memset(A, X, sizeof A)
    13                                                                 #define pb(x) push_back(x) 
    14                                                                 #define foreach(e,x) for(__typeof(x.begin()) e=x.begin();e!=x.end();++e) 
    15                                                                 #define REP(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
    16                                                                 #define rep(i,l,u) for(int (i)=(int)(l);(i)>=(int)(u);--(i))  
    17                                                                 typedef long long  LL;   
    18                                                                 typedef pair<long,long>  pll;    
    19 
    20                                                     
    21                                                                 const int mod=1e9+7; 
    22                                                                 const int maxn=1e5+10; 
    23                                                                 
    24                                                                 LL T;
    25                                                               
    26 
    27       void prime_resolve(LL mm,vector <LL> & mf,map<LL,LL> &fp)//mf m_factor,  fp factor_power  put all  the 素因子(即素数)2  ... of m  to  the  factor,且在factor按照从小到大存储.
    28                                             {
    29                                                 mf.clear();
    30                                                 for(long long  i=2;i*i<=mm;i++)
    31                                                 {
    32                                                     if(mm%i==0)
    33                                                     { 
    34                                                         mf.push_back(i);
    35                                                         while(mm%i==0)            //除干净某个因子.
    36                                                             {
    37                                                                 mm/=i;
    38                                                                 fp[i]++;
    39                                                             }
    40                                                     }
    41                                                 }
    42                                                 if(mm!=1)
    43                                                     {
    44                                                     mf.push_back(mm);  //如果m 是素数 放进去
    45                                                     fp[mm]++;
    46                                                     }
    47                                             }
    48 
    49                                             LL gcd(const LL &a,const LL &b)
    50                                                                  { return b == 0 ? a : gcd(b, a % b); } 
    51                                                                 int main()
    52                                                                     {
    53                                                                            freopen("in.txt","r",stdin); 
    54                                                                            while(scanf("%lld",&T)!=EOF&&T)
    55                                                                            {
    56 
    57                                                                             vector<LL> mf;
    58                                                                               map<LL,LL> fp;
    59                                                                               int flag=0;
    60                                                                               if(T<0)
    61                                                                                   {
    62                                                                                       flag=1;
    63                                                                                       T=-T;
    64                                                                                   } 
    65                                                                                prime_resolve(T,mf,fp);
    66                                                                                
    67                                                                                vector<LL> v;
    68                                                                                                    /*foreach(e,mp)
    69                                                                                                    {
    70                                                                                                        v.pb(it->second);
    71                                                                                                    }*/
    72                                                                                map<LL,LL> ::iterator it;
    73                                                                                for(it=fp.begin();it!=fp.end();it++)
    74                                                                                    v.pb(it->second);
    75 
    76                                                                                LL t=v[0];
    77                                                                                REP(i,1,v.size()-1)
    78                                                                                {
    79                                                                                    t=gcd(t,v[i]);
    80                                                                                }
    81                                                                                if(flag)
    82                                                                                    while(t%2==0)
    83                                                                                    {
    84                                                                                        t=t/2;
    85                                                                                    }
    86                                                                             printf("%llu
    ",t);
    87                                                                            }
    88                                                                         return 0;
    89                                                                     }
    Implement Code


    debug: 素数分解时int超出导致错误了,更改为LL.

  • 相关阅读:
    2019-6-23-win10-uwp-未给任务-GenerateAppxPackageRecipe-的必需参数-AppxManifestXml-赋值
    2018-8-17-C#-从零开始写-SharpDx-应用-控制台创建-Sharpdx-窗口
    QToolBox
    QListWidget
    宽字节 多字节 mbstowcs wcstombs
    va_start可变参数函数
    c语言二进制、八进制、十六进制
    文件锁 flock/fcntl
    volatile和锁
    串口应用程序
  • 原文地址:https://www.cnblogs.com/paulzjt/p/6040947.html
Copyright © 2011-2022 走看看