hdu 4279 Number (找规律)
http://acm.hdu.edu.cn/showproblem.php?pid=4279
题意:
给出a,b两个数,1<=a<=b 如果a,b不互质,且a%b != 0则说明a是b的特殊数字f[i]表示i拥有的特殊数字的个数,如果f[i]是奇数,那么i就是 real numbers
给出区间[x,y]求区间内的 real numbers的数量。
思路:
打标找规律,真心不好发现这规律;大于12之后 d[x] = x/2 -1或者d[x] = x/2 - 2;(d[x]表示1到x拥有的 real numbers的数量)发现在遇到平方数是转变原来-1变为-2,否则-2变为-1。 p^2 <= k < (p + 1)^2则数量为:p为奇数-1 偶数-2;
Ps:这里遇到一个很纠结的问题sqrt(x)如果不转换x为浮点型,C++出现编译错误,而G++就AC,而如果转化为浮点型c++wa..无语了。。
View Code
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <set> #include <map> #include <string> #define CL(a,num) memset((a),(num),sizeof(a)) #define iabs(x) ((x) > 0 ? (x) : -(x)) #define Min(a,b) (a) > (b)? (b):(a) #define Max(a,b) (a) > (b)? (a):(b) #define ll __int64 #define MOD 100000007 #define lc l,m,rt<<1 #define rc m + 1,r,rt<<1|1 #define pi acos(-1.0) #define test puts("<------------------->") #define maxn 100007 #define M 100007 #define N 107 using namespace std; //freopen("din.txt","r",stdin); int f[20] = {0,0,0,0,0,0,1,1,2,3,4,4,5}; ll getR(ll x){ if (x <= 12) return f[x]; ll tmp = x/2; ll tp = (ll)sqrt(1.0*x); if (tp&1) tmp -= 1; else tmp -= 2; return tmp; } int main(){ ll a,b; int t; scanf("%d",&t); while (t--){ scanf("%I64d%I64d",&a,&b); printf("%I64d\n",getR(b) - getR(a - 1)); } return 0; }