zoukankan      html  css  js  c++  java
  • hdu 4143 A Simple Problem (变形)

    题目

    题意:给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
    ", x);
    28     }
    29     return 0;
    30 }
  • 相关阅读:
    Bubble Sort (5775)
    Dertouzos (5750)
    codeforces 360 E
    codeforces 360 D
    codeforces 360 C
    Mike and Cellphone
    训练2——B
    训练1——A
    符号三角形
    Sudoku Killer
  • 原文地址:https://www.cnblogs.com/bfshm/p/3707789.html
Copyright © 2011-2022 走看看