VLATTICE - Visible Lattice Points
Consider a N*N*N lattice. One corner is at (0,0,0) and the opposite one is at (N,N,N). How many lattice points are visible from corner at (0,0,0) ? A point X is visible from point Y iff no other lattice point lies on the segment joining X and Y.
Input :
The first line contains the number of test cases T. The next T lines contain an interger N
Output :
Output T lines, one corresponding to each test case.
Sample Input :
3
1
2
5
Sample Output :
7
19
175
Constraints :
T <= 50
1 <= N <= 1000000
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1000100;
bool check[N+10];
int prime[N+10];
int mu[N+10];
void Mublus()
{
memset(check,false,sizeof(check));
mu[1]=1;
int tot=0;
for(int i=2;i<=N;i++)
{
if(!check[i])
{
prime[tot++]=i;
mu[i]=-1;
}
for(int j=0;j<tot;j++)
{
if(i*prime[j]>N) break;
check[i*prime[j]]=true;
if(i%prime[j]==0)
{
mu[i*prime[j]]=0;
break;
}
else
{
mu[i*prime[j]]=-mu[i];
}
}
}
}
int sum[N+10];
ll solve(int n)
{
ll ans=0;
// if(n>m) swap(n,m);
for(int i=1,la=0;i<=n;i=la+1)
{
la=n/(n/i);
ans+=(ll)(sum[la]-sum[i-1])*(n/i)*(n/i)*(n/i);
}
return ans;
}
ll solve1(int n)
{
ll ans=0;
for(int i=1,la=0;i<=n;i=la+1)
{
la=n/(n/i);
ans+=(ll)(sum[la]-sum[i-1])*(n/i)*(n/i);
}
return ans;
}
int main()
{
Mublus();
sum[0]=0;
for(int i=1;i<=N;i++)
sum[i]=sum[i-1]+mu[i];
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
ll ans=solve(n)+3*solve1(n);
printf("%lld\n",ans+3);
}
return 0;
}
Have you ever played Puzzle Bobble, a very famous PC game? In this game, as a very cute bobble dragon, you must keep shooting powerful bubbles to crush all the colorful bubbles upwards. Victory comes when all the bubbles upwards are crushed.
Little Tom is crazy about this game. One day, he finds that all kinds of Puzzle Bobble are 2D Games. To be more excited when playing this game, he comes up with a new idea to design a 3D Puzzle Bobble game! In this game, the bobble dragon is standing in a cubic room with L in length, W in width and H in height. Around him are so many colorful bubbles. We can use 3D Cartesian coordinates (x, y, z) to represent the locations of the bobble dragon and those bubbles. All these coordinates (x, y, z) are triple positive integers ranged from (1, 1, 1) to (L, W, H).
To simplify the problem, let's assume the bobble dragon is standing at (1, 1, 1) in the room. And there is one colorful bubble at every (x, y, z) in the room except (1, 1, 1). The dragon is so strong that he can shoot out a magical bubble to crush all the colorful bubbles in the straight line which the magical bubble flies every single time. Note that bubbles are significantly smallwith respect to the distances between each two bubbles. Our question remains, how many magical bubbles will the cute dragon shoot before crushing all the colorful bubbles around him?
Input
There are multiple cases, no more than 200. Each case contains one single line. In this line, there are three positive integers L, W and H (2 ≤ L, W, H ≤ 1000000) which describes the size of the room. Proceed to the end of the file.
Output
For each case, print the number of the magical bubbles needed to crush all the colorful bubbles in one line.
Sample Input
2 2 2 3 3 3
Sample Output
7 19
题意:有一个三维网格,长宽高分别为n,m,k,每个格点都有一棵树;问从(1,1,1)到(n ,m ,k)能看到多少棵树?
思路:可以转化为上一题的题意:从(0,0,0)看(0~n-1 , 0~m-1 ,0~ k-1)能看的多少棵树?再转化:从(0,0,0)看(1~n-1,1~m-1,1~k-1),剩下三面当作三个二维来处理;
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1000100;
bool check[N+10];
int prime[N+10];
int mu[N+10];
void Mublus()
{
memset(check,false,sizeof(check));
mu[1]=1;
int tot=0;
for(int i=2; i<=N; i++)
{
if(!check[i])
{
prime[tot++]=i;
mu[i]=-1;
}
for(int j=0; j<tot; j++)
{
if(i*prime[j]>N) break;
check[i*prime[j]]=true;
if(i%prime[j]==0)
{
mu[i*prime[j]]=0;
break;
}
else
{
mu[i*prime[j]]=-mu[i];
}
}
}
}
int sum[N+10];
ll solve(int n,int m,int k)
{
ll ans=0;
if(n>m) swap(n,m);
if(n>k) swap(n,k);
for(int i=1,la=0; i<=n; i=la+1)
{
la=min(min(n/(n/i),m/(m/i)),k/(k/i));
ans+=(ll)(sum[la]-sum[i-1])*(n/i)*(m/i)*(k/i);
}
return ans;
}
ll solve1(int n,int m)
{
ll ans=0;
if(n>m) swap(n,m);
for(int i=1,la=0; i<=n; i=la+1)
{
la=min(n/(n/i),m/(m/i));
ans+=(ll)(sum[la]-sum[i-1])*(n/i)*(m/i);
}
return ans;
}
int main()
{
Mublus();
sum[0]=0;
for(int i=1; i<=N; i++)
sum[i]=sum[i-1]+mu[i];
int n,m,k;
while(~scanf("%d%d%d",&n,&m,&k))
{
ll ans=solve(n-1,m-1,k-1)+solve1(n-1,m-1)+solve1(n-1,k-1)+solve1(m-1,k-1);
printf("%lld\n",ans+3);
}
return 0;
}