CodeForces - 58B Coins(数论)
2017-03-15 21:21
369 查看
Coins
In Berland a money reform is being prepared. New coins are being introduced. After long economic calculations was decided that the most expensive coin should possess the denomination of exactly n Berland
dollars. Also the following restriction has been introduced for comfort: the denomination of each coin should be divisible by the denomination of any cheaper coin. It is known that among all the
possible variants the variant with the largest number of new coins will be chosen. Find this variant. Print in the order of decreasing of the coins' denominations.
Input
The first and only line contains an integer n (1 ≤ n ≤ 106)
which represents the denomination of the most expensive coin.
Output
Print the denominations of all the coins in the order of decreasing. The number of coins must be the largest possible (with the given denomination n of
the most expensive coin). Also, the denomination of every coin must be divisible by the denomination of any cheaper coin. Naturally, the denominations of all the coins should be different. If there are several solutins to that problem, print any of them.
Examples
input
output
input
output
input
output
ps:其实就是分解质因子,并依次除以质因子就好了(线性筛法想了解的请戳这里)
代码:
In Berland a money reform is being prepared. New coins are being introduced. After long economic calculations was decided that the most expensive coin should possess the denomination of exactly n Berland
dollars. Also the following restriction has been introduced for comfort: the denomination of each coin should be divisible by the denomination of any cheaper coin. It is known that among all the
possible variants the variant with the largest number of new coins will be chosen. Find this variant. Print in the order of decreasing of the coins' denominations.
Input
The first and only line contains an integer n (1 ≤ n ≤ 106)
which represents the denomination of the most expensive coin.
Output
Print the denominations of all the coins in the order of decreasing. The number of coins must be the largest possible (with the given denomination n of
the most expensive coin). Also, the denomination of every coin must be divisible by the denomination of any cheaper coin. Naturally, the denominations of all the coins should be different. If there are several solutins to that problem, print any of them.
Examples
input
10
output
10 5 1
input
4
output
4 2 1
input
3
output
3 1
ps:其实就是分解质因子,并依次除以质因子就好了(线性筛法想了解的请戳这里)
代码:
#include<stdio.h>
#define maxn 1000000+10#define max(a,b) (a>b?a:b)
int p[maxn/10],c[maxn];
void is_prim()//线性筛法
{
int tot=0,i,j;
c[0]=1,c[1]=1;
for(i=2; i<=maxn; i++)
{
if(!c[i])
p[tot++]=i;
for(j=0; j<tot&&i*p[j]<maxn; j++)
{
c[i*p[j]]=1;
if(!(i%p[j]))
break;
}
}
}
int main()
{
is_prim();
int n;
scanf("%d",&n);
printf("%d ",n);
int ps=0;
while(n!=1)
{
if((n%p[ps])==0)
{
n/=p[ps];
printf("%d ",n);
}
else
ps++;
}
return 0;
}
相关文章推荐
- CodeForces - 58B Coins(数论)
- CodeForces - 58B Coins(数论)
- CodeForces - 58B Coins(数论)
- CodeForces - 58B Coins(数论)
- CodeForces - 58B Coins(数论)
- CodeForces - 58B Coins(数论)
- CodeForces - 58B Coins(数论)
- CodeForces - 58B Coins(数论)
- CodeForces - 58B Coins(数论)
- CodeForces - 58B Coins(数论)
- CodeForces - 58B Coins(数论)
- CodeForces - 58B Coins(数论)
- CodeForces - 58B Coins(数论)
- codeforces 487C C. Prefix Product Sequence(构造+数论)
- codeforces 396A A. On Number of Decompositions into Multipliers(组合数学+数论)
- CodeForces - 376C Divisible by Seven(数论:同余定理)(找规律)
- CodeForces - 359C Prime Number(数论)
- codeforces 901B GCD of Polynomials (数论+构造)
- CodeForces 75 C.Modified GCD(数论)
- codeforces 453B B. Little Pony and Harmony Chest(dp+数论)