首页 > 代码库 > UVA - 11014 Make a Crystal
UVA - 11014 Make a Crystal
Description
Problem C
Make a Crystal
Input: Standard Input
Output: StandardOutput
A scientist is trying hard to make a very large crystal, a largecrystal of Carbon to be specific. He believes, as Diamond is a crystal ofCarbon and very precious, so his new crystal of carbon would be as precious asDiamond in the long run. The atoms in his crystal will not hold togethernaturally, so he wants to put a strong force at the center of the crystal,which will attract all the carbon atoms and keep them together.
The Carbon atoms in a diamond crystal can be considered to be placed ina cube (Shown inFigure2). The scientist alsowants to place the carbon atoms of his crystal in an(N x N x N) cube, whereN is an even number. If the center of thiscube is(0,0,0) and all the sides of this cube are parallel toxy,yz orxz planethen all the atoms will be placed in three-dimensional integer coordinates. Soif(x, y, z) is thecoordinate of an atom placed in an(N*N*N) cube thenx,y, and z are integers and (-N/2<=x, y,z<=N/2). Asthe strong force at the center will attract all the atoms so the atoms areplaced in such a way so that no atom is between the center and another atom.For example if there is an atom at coordinate(2, 2, 2) then no atoms should be placed incoordinate(1, 1, 1) asthen the atom at(1, 1, 1)will block the attractive force between(2, 2, 2) and the center. Similarly if there is anatom at place(1, 1, 1) then there should be no atom in location(2,2, 2).Figure 3 shows such an arrangement for(4x4x4) cube: if atoms are placed in all integercoordinates (the value ofx,y andz are integers) except those marked withblack circles, no atom will be between the center and another atom. Given thesize of the cube (length of one side) in which atoms are to be placed to makethe crystal, your job is to find out the maximum number of atoms that can beplaced following the constraints mentioned above
Input
The input file contains at most 30 lines of inputs. Each line contains an even integer N (0<N<=200000), which indicates the length of one side of the cube inwhich the scientist plans to put his atoms. Input is terminated by a line wherethe value of N is zero.
Output
For each lineof input except the last one produce one line of output. This line shouldcontain the serial of output followed by an integer, which denotes the maximumnumber of atoms that can be placed.
SampleInput Outputfor Sample Input
4 2 0 | Crystal 1: 98Crystal 2: 26 |
Problem setter: Shahriar Manzoor, EPS
Special Thanks: Derek Kisman,EPS
题意:一个N*N*N的立方体,坐标范围是-N/2<=x,y,z<=N/2,你的任务是选择尽量多的网格点,使得对于任意两个选出的P和Q,OPQ不共线,输出最大的点数
思路:设f(k)表示点(x,y,z)都是都是k的倍数的情况数,那么所有的情况就是f(1)了,然后处理共线的可能,但是在枚举k的过程可能会有重复计算,比如f(6)就会被f(2)和f(3)计算过,所以需要用到容斥原理,对于奇数个因子我们选择减,偶数个选择加,结果是:f(1) - f(2) - f(3) - f(5) - f(7) - ... + f(2*3) + f(2*5) + f(3*5) + ... - f(2*3*5) - f(2*3*7)...
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> typedef long long ll; using namespace std; const int maxn = 200005; ll n, prime[maxn]; int vis[maxn], cnt; void init() { vis[1] = 1; cnt = 0; for (ll i = 2; i < maxn; i++) { if (vis[i]) continue; prime[cnt++] = i; for (ll j = i*i; j < maxn; j += i) vis[j] = 1; } } ll pow3(ll x) { return x * x * x - 1; } int count(ll x) { int ans = 0; for (int i = 0; i < cnt && prime[i] <= x; i++) { if (!vis[x]) { ans++; break; } if (x % prime[i] == 0) { ans++; x /= prime[i]; if (x % prime[i] == 0) return -1; } } return ans; } ll cal(ll x) { int cnt = count(x); if (cnt == -1) return 0; if (cnt & 1) return -pow3((n / 2 / x) * 2 + 1); else return pow3((n / 2 / x) * 2 + 1); } ll solve() { ll ans = pow3(n+1); for (ll i = 2; i <= n; i++) ans += cal(i); return ans; } int main() { init(); int cas = 1; while (scanf("%lld", &n) != EOF && n) { printf("Crystal %d: %lld\n", cas++, solve()); } return 0; }
UVA - 11014 Make a Crystal