标签:style blog class code java tar
题意:给n,求x;
直接枚举肯定超时, 把给的式子变形, (y+x)(y-x) = n;
令y-x = b, y+x = a;
枚举b, b 的范围肯定是sqrt(n), y = (a+b)/2; x = (a-b)/2;
b越大, x越小, 所以倒着枚举b
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 using namespace std; 6 7 int main() 8 { 9 int t, n, a, b, x; 10 scanf("%d", &t); 11 while(t--) 12 { 13 scanf("%d", &n); 14 x = -1; 15 for(b = sqrt(n); b >= 1; b--) 16 { 17 if(n%b==0) 18 { 19 a = n/b; 20 if(a>b && (a-b)%2==0) 21 { 22 x = (a-b)/2; 23 break; 24 } 25 } 26 } 27 printf("%d\n", x); 28 } 29 return 0; 30 }
hdu 4143 A Simple Problem (变形),布布扣,bubuko.com
hdu 4143 A Simple Problem (变形)
标签:style blog class code java tar
原文地址:http://www.cnblogs.com/bfshm/p/3707789.html