【Codeforces Round 326 (Div 2)B】【质因数分解】Duff in Love n的最大因子使其不为平方数倍数
2015-11-10 10:18
399 查看
B. Duff in Love
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Duff is in love with lovely numbers! A positive integer x is
called lovely if and only if there is no such positive integer a > 1 such
that a2 is
a divisor of x.
Malek has a number store! In his store, he has only divisors of positive integer n (and
he has all of them). As a birthday present, Malek wants to give her a lovely number from his store. He wants this number to be as big as possible.
Malek always had issues in math, so he asked for your help. Please tell him what is the biggest lovely number in his store.
Input
The first and only line of input contains one integer, n (1 ≤ n ≤ 1012).
Output
Print the answer in one line.
Sample test(s)
input
output
input
output
Note
In first sample case, there are numbers 1, 2, 5 and 10 in the shop. 10 isn't divisible by any perfect square, so 10 is lovely.
In second sample case, there are numbers 1, 2, 3, 4, 6 and 12 in the shop. 12 is divisible by 4 = 22,
so 12 is not lovely, while 6 is indeedlovely.
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=0,M=0,Z=1e9+7,ms63=1061109567;
int casenum,casei;
int main()
{
LL n;
while(~scanf("%lld",&n))
{
int top=sqrt(n);
LL ans=1;
for(int i=2;i<=top;i++)if(n%i==0)
{
ans*=i;
while(n%i==0)n/=i;
}
if(n)ans*=n;
printf("%lld\n",ans);
}
return 0;
}
/*
【题意】
给你一个n([1,1e12]),
我们想要找到n中的最大的一个因子,使得这个因子不是任何平方数的倍数。
【类型】
质因数分解
【分析】
不是任何平方数的倍数,等价于不是任何素数的倍数。
不是任何素数的倍数,等价于任何素数这个数都最多只含有一个。
于是我们对n做素数拆分,所有其因子素数的单次乘积结果,就是答案啦。
【时间复杂度&&优化】
O(sqrt(n))
*/
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Duff is in love with lovely numbers! A positive integer x is
called lovely if and only if there is no such positive integer a > 1 such
that a2 is
a divisor of x.
Malek has a number store! In his store, he has only divisors of positive integer n (and
he has all of them). As a birthday present, Malek wants to give her a lovely number from his store. He wants this number to be as big as possible.
Malek always had issues in math, so he asked for your help. Please tell him what is the biggest lovely number in his store.
Input
The first and only line of input contains one integer, n (1 ≤ n ≤ 1012).
Output
Print the answer in one line.
Sample test(s)
input
10
output
10
input
12
output
6
Note
In first sample case, there are numbers 1, 2, 5 and 10 in the shop. 10 isn't divisible by any perfect square, so 10 is lovely.
In second sample case, there are numbers 1, 2, 3, 4, 6 and 12 in the shop. 12 is divisible by 4 = 22,
so 12 is not lovely, while 6 is indeedlovely.
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=0,M=0,Z=1e9+7,ms63=1061109567;
int casenum,casei;
int main()
{
LL n;
while(~scanf("%lld",&n))
{
int top=sqrt(n);
LL ans=1;
for(int i=2;i<=top;i++)if(n%i==0)
{
ans*=i;
while(n%i==0)n/=i;
}
if(n)ans*=n;
printf("%lld\n",ans);
}
return 0;
}
/*
【题意】
给你一个n([1,1e12]),
我们想要找到n中的最大的一个因子,使得这个因子不是任何平方数的倍数。
【类型】
质因数分解
【分析】
不是任何平方数的倍数,等价于不是任何素数的倍数。
不是任何素数的倍数,等价于任何素数这个数都最多只含有一个。
于是我们对n做素数拆分,所有其因子素数的单次乘积结果,就是答案啦。
【时间复杂度&&优化】
O(sqrt(n))
*/
相关文章推荐
- 简单的四则运算
- 数的奇偶性
- ACM网址
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 矩阵的乘法操作
- 蚂蚁爬行问题
- 蚂蚁爬行问题
- 求两个数的最大公约数【ACM基础题】
- 打印出二进制中所有1的位置
- 杭电题目---一只小蜜蜂
- HDOJ 1002 A + B Problem II (Big Numbers Addition)
- 初学ACM - 半数集(Half Set)问题 NOJ 1010 / FOJ 1207
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1002
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points