codeforces 558/C Amr and Chemistry(数论+位运算)

时间:2024-03-27 12:35:26

题目链接:http://codeforces.com/problemset/problem/558/C

题意:把n个数变成相同所需要走的最小的步数
易得到结论,两个奇数不同,一直×2不可能有重叠
枚举每个数可能到得所有值,以及统计达到该值的时候已经走的步数
最终答案就是1到up中num[i]最小的数

Examples
input output
2 input output
5 Note
In the first sample test, the optimal solution is to divide the second chemical volume by two, and multiply the third chemical volume by two to make all the volumes equal . In the second sample test, the optimal solution is to divide the first chemical volume by two, and divide the second and the third chemical volumes by two twice to make all the volumes equal .

***********************************************************

AC代码:

 #include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<stdlib.h> using namespace std; #define N 5200
#define INF 0x3f3f3f3f
#define Maxn 100010 int a[Maxn],step[Maxn],num[Maxn]; int main()
{
int n,i; while(scanf("%d", &n) != EOF)
{
memset(step,,sizeof(step));
memset(num,,sizeof(num)); for(i=; i<=n; i++)
scanf("%d", &a[i]); for(i=; i<=n; i++)
{
int x=a[i],sc=; while(x)
{
int s1=;
while(x%==)
{
x/=;
s1++;
} int y=x,s2=;
while(y<=Maxn)
{
num[y]++;
step[y]+=sc+abs(s1-s2);
s2++;
y*=;
} sc+=s1+;
x/=;
}
}
int ans=INF;
for(i=;i<=Maxn;i++)
if(num[i]==n)
ans=min(ans,step[i]); printf("%d\n", ans);
}
return ;
}

这个里写的很清楚:http://blog.csdn.net/u014028317/article/details/46897963