码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 4143 A Simple Problem (变形)

时间:2014-05-05 11:01:25      阅读:244      评论:0      收藏:0      [点我收藏+]

标签: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

bubuko.com,布布扣
 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 }
bubuko.com,布布扣

 

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!