ZOJ 3435

求(1,1,1)至(x,y,z)的互质个数。

即求(0,0,0)到(x-1,y-1,z-1)互质个数。

依然如上题那样做。但很慢。。。好像还有一个分块的思想,得学学。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 1000005

using namespace std;
typedef long long LL;
int mobi[N];
bool vis[N];

void initial(){
    int i,j;
    for(i=1;i<N;i++) mobi[i]=1,vis[i]=false;
    for(i=2;i<N;i++) {
        if(vis[i]) continue;
        for(j=i;j<N;j+=i){
            vis[j]=true;
            if((j/i)%i==0){
                mobi[j]=0; continue;
            }
            mobi[j]=-mobi[j];
        }
    }
}

int main(){
	initial();
	int t,x,y,z;
	while(scanf("%d%d%d",&x,&y,&z)!=EOF){
		x--; y--; z--;
		t=max(max(x,y),z);
		LL ans=0;
		for(int i=1;i<=t;i++){
			ans+=((LL)mobi[i]*(LL)(x/i)*(LL)(y/i)*(LL)(z/i)+(LL)mobi[i]*(LL)(x/i)*(LL)(y/i)+(LL)mobi[i]*(LL)(x/i)*(LL)(z/i)+(LL)mobi[i]*(LL)(y/i)*(LL)(z/i));
		}
		ans+=3;
		printf("%lld\n",ans);
	}
	return 0;
}

  

时间: 2024-10-05 04:33:50

ZOJ 3435的相关文章

zoj 3435 spoj 7001 莫比乌斯反演

zoj 3435 题意: 给出3个数a,b,c, 定义一个立方体,这个立方体有a*b*c个点,每个点的坐标都是整数(x,y,z),求经过坐标(1,1,1)和另外任意一个点(x1,y1,z1)的不同的直线有多少条. 限制: 2 <= a,b,c <= 1e6; 有200组数据. 思路: 有3种情况: 1. x1,y1,z1都大于等于2: 问题就变成求1 <= x <= a-1 && 1 <= y <= b-1 && 1 <= z &l

ZOJ 3435 Ideal Puzzle Bobble

ZOJ Problem Set - 3435 Ideal Puzzle Bobble Time Limit: 2 Seconds      Memory Limit: 65536 KB 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 t

csu 2014 summer training day 2 莫比乌斯反演

SPOJ VLATTICE 题意:x,y,z<=1000000,x<=a,y<=b,z<=c,给定a.b.c,求gcd(x,y,z)=1的个数 解释:设 f(n)是gcd(x,y,z)=n的种数,F(n)=n|gcd(x,y,z)的种数 那么F(n)=f(n)+f(2n)....=sigm(f(d)){n|d} 那么根据反演公式 f(n)=sigm(u(d/n)*F(d)){n|d} 我们要求的是f(1)=sigm(u(1)*F(n)+u(2)*F(2n)+u(3)*F(3n)..

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

概率dp ZOJ 3640

Help Me Escape Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3640 Appoint description:  System Crawler  (2014-10-22) Description Background     If thou doest well, shalt thou not be accepted? an

zoj 2156 - Charlie&#39;s Change

题目:钱数拼凑,面值为1,5,10,25,求组成n面值的最大钱币数. 分析:dp,01背包.需要进行二进制拆分,否则TLE,利用数组记录每种硬币的个数,方便更新. 写了一个 多重背包的 O(NV)反而没有拆分快.囧,最后利用了状态压缩优化 90ms: 把 1 cents 的最后处理,其他都除以5,状态就少了5倍了. 说明:貌似我的比大黄的快.(2011-09-26 12:49). #include <stdio.h> #include <stdlib.h> #include <

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio

ZOJ 3607 Lazier Salesgirl (贪心)

Lazier Salesgirl Time Limit: 2 Seconds      Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making and selling. She can sell the i-th customer a piece of bread for price pi. But she is so lazy

ZOJ - 2243 - Binary Search Heap Construction

先上题目: Binary Search Heap Construction Time Limit: 5 Seconds      Memory Limit: 32768 KB Read the statement of problem G for the definitions concerning trees. In the following we define the basic terminology of heaps. A heap is a tree whose internal n