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\n", x);
28 }
29 return 0;
30 }

hdu 4143 A Simple Problem (变形),布布扣,bubuko.com

时间: 2025-01-31 08:41:26

hdu 4143 A Simple Problem (变形)的相关文章

HDU 4143 A Simple Problem(数论-水题)

A Simple Problem Problem Description For a given positive integer n, please find the smallest positive integer x that we can find an integer y such that y^2 = n +x^2. Input The first line is an integer T, which is the the number of cases. Then T line

HDU 4143 A Simple Problem(枚举)

题目链接 题意 : 就是给你一个数n,让你输出能够满足y^2 = n +x^2这个等式的最小的x值. 思路 : 这个题大一的时候做过,但是不会,后来学长给讲了,然后昨天比赛的时候二师兄看了之后就敲了,我也想了一会儿才想起来,真是惭愧啊..... 其实就是将上边那个式子变一下:(y-x)*(y+x) = n ,然后接下来就去枚举(y-x)的值,因为手算了几组数据,发现当这个值越靠近√n时,x的值越小,其实看这个等式也可以看出来,所以枚举的时候从√n这里开始往下枚举就行,然后再看(y-x)+(y+x

hdu 4267 A Simple Problem with Integers

题目链接:hdu 4267 A Simple Problem with Integers 类似于题目:hdu 1556 Color the ball 的技巧实现树状数组的段更新点查询. 由于该题对于段的更新并不是连续的,从而可以构造多个树状数组.因为$k \in [1,10] $,从而可以把更新划分为如下类型: 1,2,3,4,5... ------------- 1,3,5,7,9... 2,4,6,8,10... ------------- 1,4,7,10,13... 2,5,8,11,1

HDU 2522 A simple problem

A simple problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3368    Accepted Submission(s): 1249 Problem Description Zty很痴迷数学问题..一天,yifenfei出了个数学题想难倒他,让他回答1 / n.但Zty却回答不了^_^.  请大家编程帮助他. In

hdu 4267 A Simple Problem with Integers(树形结构-线段树)

A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3708    Accepted Submission(s): 1139 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with

HDU 4267 A Simple Problem with Integers(树状数组区间更新)

A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5402    Accepted Submission(s): 1710 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with

HDU 4267 A Simple Problem with Integers (树状数组)

A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Description Let A1, A2, ... , AN be N elements. You need to deal with two kinds of operations. One type of operation is to add a given

HDU 4267 A Simple Problem with Integers(树状数组)

A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4191    Accepted Submission(s): 1309 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with

HDU ACM 4143 A Simple Problem

分析:y^2=n+x^2=>y^2-x^2=n. (y-x)(y+x)=n. 令k1=y-x:k2=y+x. 则有:y=(k1+k2)/2,x=y-k1. 枚举n的所有因数k1,k2使得y为整数.则最小的x即为所求. 注意:x不能为0. #include<iostream> #include<cmath> using namespace std; void Solve(int n) { int i,x,y,minx; minx=0x7fffffff; for(i=1;i<