HDU 4279 Number(找规律)

原题:http://acm.hdu.edu.cn/showproblem.php?pid=4279

Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2819    Accepted Submission(s): 774


Problem Description
  Here are two numbers A and B (0 < A <= B). If B cannot be divisible by A, and A and B are not co-prime numbers, we define A as a special number of B.
  For each x, f(x) equals to the amount of x’s special numbers.
  For example, f(6)=1, because 6 only have one special number which is 4. And f(12)=3, its special numbers are 8,9,10.
  When f(x) is odd, we consider x as a real number.
  Now given 2 integers x and y, your job is to calculate how many real numbers are between them.
 

Input
  In the first line there is an integer T (T <= 2000), indicates the number of test cases. Then T line follows, each line contains two integers x and y (1 <= x <= y <= 2^63-1) separated by a single space.
 

Output
  Output the total number of real numbers.
 

Sample Input
  
  
2 1 1 1 10
 

Sample Output
  
  
0 4
题意:找出在给定范围内 real numbers的个数

打表:

#include <stdio.h>

int gcd(int a,int b)
{
	if(b==0)
		return a;
	return gcd(b,a%b);
}

int main()//打表找规律
{
	y=1000;
	for(i=1;i<=y;i++)
	{
		num=0;
		for(j=1;j<i-1;j++)
		{
			m=gcd(i,j);
			if(m!=j&&m>1)
				num++;
		}
		if(num%2)
			printf("%d\n",i);
	}
	return 0;
}


AC代码:

#include <stdio.h>
#include <math.h>

#define IN __int64
/*
author:YangSir
time:2014/5/8
*/
IN f(IN a)
{
	IN m,b;
	m=(a-4)/2;//除去2,4的偶数个数
	b=sqrt(a);
	if(b%2)//比较偶数开方后的偶数和奇数平方后的奇数个数是否相等
		return m+1;//不等
	else
		return m;//相等
}

int main()
{
	IN x,y;
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%I64d%I64d",&x,&y);
		if(y<6)
		{
			printf("0\n");
			continue;//因为y比x大,所以y没有x也就没有
		}
		if(x<6)
		{
			printf("%I64d\n",f(y));
		}
		else
		{
			printf("%I64d\n",f(y)-f(x-1));//因为是闭区间,取得到x
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值