Codeforces Round #368 DIV2 C.
简单题意:给出x(x<=1e9),求一组包含x的勾股数
我好像没什么思路啊,先分类讨论吧
首先x是斜边:
x^2 = a^2+b^2
似乎除了枚举a没有什么好办法
再试试x是直角边
a^2+x^2=b^2
试着把式子变形
(b+a)(b-a) = x^2
这个就好办了,我们考虑把x^2分成两个数的积,显然只要这两个数奇偶性相同并且不相等,我们就能找到一组合法的a,b
然后你发现,除了x=1和x=2都有解,如果x是偶数先把x=x/2分给a,b各一个2,然后分成x^2*1即可
1 #include <iostream> 2 #include <cstdlib> 3 #include <cstdio> 4 #include <algorithm> 5 #include <string> 6 #include <cstring> 7 #include <cmath> 8 #include <map> 9 #include <stack> 10 #include <set> 11 #include <vector> 12 #include <queue> 13 #include <time.h> 14 #define eps 1e-7 15 #define INF 0x3f3f3f3f 16 #define MOD 1000000007 17 #define rep0(j,n) for(int j=0;j<n;++j) 18 #define rep1(j,n) for(int j=1;j<=n;++j) 19 #define pb push_back 20 #define set0(n) memset(n,0,sizeof(n)) 21 #define ll long long 22 #define ull unsigned long long 23 #define iter(i,v) for(edge *i=head[v];i;i=i->nxt) 24 #define print_runtime printf("Running time:%.3lfs\n",double(clock())/1000.0) 25 #define TO(j) printf(#j": %d\n",j) 26 //#define OJ 27 using namespace std; 28 const int MAXINT = 100010; 29 const int MAXNODE = 100010; 30 const int MAXEDGE = 2*MAXNODE; 31 char BUF,*buf; 32 int read(){ 33 char c=getchar();int f=1,x=0; 34 while(!isdigit(c)){if(c=='-') f=-1;c=getchar();} 35 while(isdigit(c)){x=x*10+c-'0';c=getchar();} 36 return f*x; 37 } 38 char get_ch(){ 39 char c=getchar(); 40 while(!isalpha(c)) c=getchar(); 41 return c; 42 } 43 //------------------- Head Files ----------------------// 44 45 ll x; 46 void get_input(); 47 void work(); 48 int main() { 49 get_input(); 50 work(); 51 return 0; 52 } 53 void work(){ 54 ll a=1,b=1,k,m; 55 if(x<=2) {printf("-1\n");return ;} 56 if(x%2==0) a*=2,b*=2,x/=2; 57 x=x*x; 58 a*=x; 59 k=(a+b)/2; 60 m=(a-b)/2; 61 printf("%lld %lld\n",m,k); 62 } 63 void get_input(){ 64 x=read(); 65 }
许愿弥生改二