# 2017ccpc哈尔滨站题解合集


A Simple Stone Game
Time Limit: 2000/1000 MS(Java/Others)    Memory Limit: 262144/262144 K(Java/Others)
Total Submission(s): 524    Accepted Submission(s): 107

Problem Description
After he has learned how toplay Nim game, Bob begins to try another stone game which seems much easier.

The game goes like this: one player starts the game with N piles of stones. There is ai stoneson the i thpile. On one turn, the player can move exactly one stone from one pile toanother pile. After one turn, if there exits a number x(x>1) such that for each pile bi isthe multiple of x where bi is the number of stone of thethis pile now), the game will stop. Now you need to help Bob to calculate theminimum turns he need to stop this boring game. You can regard that 0 is the multiple of anypositive number.

Input
The first line is the numberof test cases. For each test case, the first line contains one positive number N(1≤N≤100000), indicating the number ofpiles of stones.

The second line contains N positive number, the i th number ai(1≤ai≤100000) indicating the number ofstones of the i th pile.

The sum of N ofall test cases is not exceed 5∗105.

Output
For each test case, output ainteger donating the answer as described above. If there exist a satisfiednumber x initially,you just need to output 0. It's guaranteed that there exists at least one solution.

Sample Input
2
5
1 2 3 4 5
2
5 7

Sample Output
2
1

【题意】

【思路】

【PS】 注意用long long型

#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)

typedef long long ll;
const int maxn = 100005;
const ll mod = 10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;

int n;
ll a[maxn];
vector<ll>vec;
vector<ll>mp;

int main()
{
rush()
{
scanf("%d",&n);
vec.clear();
ll sum=0;
for(int i=0;i<n;i++)
{
scanf("%I64d",&a[i]);
sum+=a[i];
}
for(ll i=2;i*i<=sum;i++)           //求出总和的质因子
{
if(sum%i==0)
{
vec.push_back(i);
while(sum%i==0)
{
sum/=i;
}
}
}
if(sum>1) vec.push_back(sum);
sort(vec.begin(),vec.end());
ll ans=1e18;
for(int i=0;i<vec.size();i++)           //枚举质因子
{
sum=0;
mp.clear();
for(int j=0;j<n;j++)
{
int tmp=a[j]%vec[i];
if(tmp)
{
mp.push_back(tmp);
sum+=tmp;                 //距离倍数的总差距
}
}
sort(mp.begin(),mp.end());
ll cnt=0;
//从接近倍数的石子堆开始考虑，不从小的开始考虑的原因是不确定这堆是否被拿去石子
for(int j=mp.size()-1;j>=0;j--)
{
cnt+=vec[i]-mp[j];            //变成倍数需要几颗石子，即操作次数
sum-=vec[i];                  //每次能使总差距减少vec[i]
if(sum<=0) break;
}
ans=min(ans,cnt);
}
printf("%I64d\n",ans);
}
return 0;
}  
 2017CCPC哈尔滨 F：Permutation（构造）

OK

//2017CCPC哈尔滨--F
#include<stdio.h>
int a[100005];
int main(void)
{
int T, n, i, now;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
now = 0;
for(i=1;i<=n;i+=2)
a[i] = ++now;
for(i=2;i<=n;i+=2)
a[i] = ++now;
printf("%d", a[1]);
for(i=2;i<=n;i++)
printf(" %d", a[i]);
printf("\n");
}
return 0;
}

2017CCPC哈尔滨 D：X-Men

[cpp] view plain copy
//2017CCPC哈尔滨--D
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> G[1005];
int ans, k[1005];
void Sech(int u, int p, int len)
{
int i, v;
if(k[u])
ans = max(ans, len);
for(i=0;i<G[u].size();i++)
{
v = G[u][i];
if(v==p)
continue;
Sech(v, u, len+1);
}
}
int main(void)
{
int T, n, m, x, y, i;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &m);
for(i=1;i<=n;i++)
G[i].clear();
memset(k, 0, sizeof(k));
for(i=1;i<=m;i++)
{
scanf("%d", &x);
k[x] = 1;
}
for(i=1;i<=n-1;i++)
{
scanf("%d%d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
ans = 0;;
for(i=1;i<=n;i++)
{
if(k[i])
Sech(i, 0, 0);
}
printf("%d.00\n", ans/2);
}
return 0;
}  
题意：给你n个点，让你找一个圆心和一个半径，使得已知的n个点中至少有(n+1)/2个点在圆上，

[cpp] view plain copy
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define maxn 100005
#define eps 1e-6
struct node1
{
double x,y,r;
}a[maxn];
int n;
double dis(node1 a,node1 b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool eq(double a,double b)
{
if(fabs(a-b)<=eps)
return 1;
return 0;
}
bool gongxian(node1 a,node1 b,node1 c)//判断三点是否共线
{
if(eq((b.x-a.x)*(c.y-a.y),(c.x-a.x)*(b.y-a.y)))
return 1;
return 0;
}
node1 yuanxin(node1 a,node1 b,node1 c)//求圆心坐标
{
double a1=b.x-a.x,b1=b.y-a.y,c1=(a1*a1+b1*b1)/2;
double a2=c.x-a.x,b2=c.y-a.y,c2=(a2*a2+b2*b2)/2;
double d=a1*b2-a2*b1;
node1 res;
res.x=a.x+(c1*b2-c2*b1)/d;
res.y=a.y+(a1*c2-a2*c1)/d;
return res;
}
bool judge(node1 c)
{
int num=0;
for(int i=0;i<n;i++)
if(eq(c.r,dis(c,a[i])))
num++;
if(num>=(n+1)/2)
return 1;
return 0;
}
int main(void)
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%lf%lf",&a[i].x,&a[i].y);
if(n==1)
printf("%lf %lf %lf\n",a[0].x+1,a[0].y,1.0);
else if(n<=4)
{
node1 ans;
ans.x=a[0].x+a[1].x;ans.y=a[0].y+a[1].y;
printf("%lf %lf %lf\n",ans.x/2.0,ans.y/2.0,dis(a[0],a[1])/2.0);
}
else
{
while(1)
{
int t1,t2,t3;
t1=rand()%n;
t2=rand()%n;
t3=rand()%n;
if(gongxian(a[t1],a[t2],a[t3]))
continue;
node1 c=yuanxin(a[t1],a[t2],a[t3]);
c.r=dis(c,a[t1]);
if(judge(c))
{
printf("%lf %lf %lf\n",c.x,c.y,c.r);
break;
}
}
}
}
return 0;
}



©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客