问题 H: x*x+y*y=z*z
问题 H: x*x+y*y=z*z
时间限制: 1 Sec 内存限制: 128 MB 提交: 109 解决: 5 [提交][状态][讨论版]题目描述
Given an odd positive integer x, calculate the smallest y, such that there is a positive integer z satisfying x*x+y*y=z*z and gcd(x,y)=1.Note that gcd(x,y)=1 means the greatest common divisor of x and y is 1.
输入
The input consists of multiple test cases. For each case, the only line contains an integer x(2<=x<=100,000,000).
输出
For each case,print a line with y.If there is no such y,just output -1.Note that the answer may exceed 32-bit integer.
样例输入
样例输出
提示
1 #pragma comment(linker,"/STACK:102400000,102400000") 2 #include <cstdio> 3 #include <vector> 4 #include <cmath> 5 #include <stack> 6 #include <queue> 7 #include <cstring> 8 #include <iostream> 9 #include <algorithm> 10 using namespace std; 11 #define INF 0x7fffffff 12 #define mod 1000000007 13 #define ll long long 14 #define maxn 10006 15 #define pi acos(-1.0) 16 #define FF(i,n) for(int i=0;i<n;i++) 17 int n, m, z, t, flag, k; 18 ll c,d,y,x; 19 int a[maxn]; 20 ll gcd(ll n, ll m){ return m ? gcd(m, n%m) : n; } 21 int main(){ 22 while (scanf("%lld", &x)!=EOF){ 23 k = 0; y = (1LL << 60); 24 for (int i = 1; i*i <= x; i++)if (x%i == 0)a[k++] = i; 25 for (int i = 0; i < k; i++) 26 for (int j = 0; j < k; j++){ 27 c = a[i] * a[j]; d = x*x / c; 28 if (d!=c&&(d - c) % 2 != 0||(c + d) / 2 <= x)continue; 29 if (gcd(x, (d - c) / 2) == 1)y = min(y, (d - c) / 2); 30 } 31 printf("%lld\n", y); 32 } 33 return 0; 34 }
A Simple Problem
Time Limit: 2000/1000 MS (Java/Others)
1 #include <cstdio> 2 #include <cmath> 3 #include <algorithm> 4 using namespace std; 5 #define ll long long 6 int n,x,k; 7 int a[20005]; 8 int main(){ 9 int T; scanf("%d", &T); 10 while (T--){ 11 scanf("%d", &n); 12 x = n; 13 k = 1; 14 a[0] = 1; 15 for (int i = 2; i*i <= x; i++)if (x%i == 0)a[k++] = i; 16 a[k++] = n; 17 x = -1; 18 for (int i = k-1; i>=0; i--) 19 if (n/a[i]>a[i]&&(n / a[i]-a[i]) % 2 == 0){ 20 x = (n / a[i]-a[i]) / 2; 21 break; 22 } 23 printf("%d\n", x); 24 } 25 return 0; 26 }
1041: [HAOI2008]圆上的整点
Time Limit: 10 Sec Memory Limit: 162 MB Submit: 1785 Solved: 752 [Submit][Status]Description
求一个给定的圆(x^2+y^2=r^2),在圆周上有多少个点的坐标是整数。
Input
r
Output
整点个数
Sample Input
Sample Output
HINT
n<=2000 000 000
Source
本来还想一起弄下这个题的,结果智商不够,数学也太差。。。
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 #include <map> 5 #include <cmath> 6 #include <iostream> 7 #include <algorithm> 8 using namespace std; 9 #define ll long long 10 ll n, m, s, t, k, r; 11 int a[10009]; 12 int main(){ 13 cin >> r; 14 for (int i = 1; i*i <= r; i++){ 15 if (r%i)continue; 16 ll tmp; 17 for (int p = 1; p <= 2; p++){ 18 if (p == 1)tmp = i; 19 else tmp = r/i; 20 for (n = 1; n*n <= tmp; n++){ 21 int m = sqrt(1.0*tmp - n*n); 22 if (m*m + n*n != tmp || m < n)continue; 23 ll x = m*m - n*n; 24 ll y = 2 * n*m; 25 if (x&&y&&x*x + y*y == tmp*tmp){ 26 int t = r / tmp; 27 x *= t; y *= t; 28 a[(x + y + r) % 10007] = 1; 29 } 30 } 31 } 32 } 33 for (int i = 0; i <= 10007; i++)s += a[i]; 34 cout << s * 8 + 4 << endl; 35 return 0; 36 }
原来我只是少算了r/i的这种情况。。。
笨的。。。
虽然不怎么会写弄hash,不过一点点思想就可以了。。。
因为总的圆上的整点就不多。所以状态几乎不会有偶然重复情况。。。直接把(x+y+r)%10007作为标记就可以了。。。
而且好像map写的不好会超时。。。可是我连map存储2维key都不知道怎么写。。。菜的。。。
要我直接弄hash 0 0也是压力大。。。
还是加上下壕的hash主要代码吧。。。
1 //struct Hash{ 2 // int next; 3 // int num, val; 4 //}hsh[maxn+maxn]; 5 //bool hflag[maxn]; 6 //int top = maxn; 7 //void ins(int x, int val){ 8 // int key = x&(maxn - 1); 9 // if (!hflag[key]){ 10 // hflag[key] = 1; 11 // hsh[key].next = -1; 12 // hsh[key].num = x; 13 // hsh[key].val = val; 14 // return; 15 // } 16 // while (hsh[key].next != -1)key = hsh[key].next; 17 // hsh[key].next = ++top; 18 // hsh[top].next = -1; 19 // hsh[top].num = x; 20 // hsh[top].val = val; 21 //} 22 //int inhash(int x){ 23 // int key = x&(maxn - 1); 24 // if (!hflag[key])return -1; 25 // while (key != -1){ 26 // if (hsh[key].num == x)return hsh[key].val; 27 // key = hsh[key].next; 28 // } 29 // return -1; 30 //}
H - Salmon And Cat
Problem Description
Math is very important, for those who are also in school, make sure you will learn more about math.
Salmon and Cat are good friends.Today Salmon ask Cat to help her judge whether a number is perfect or not. Perfect number is a kind of number defined by like this.
First, 1 and 3 are perfect number.Then if a and b are perfect numbers, 2+ab+2a+2b is also a perfect number.For example, 1 and 1 are perfect numbers, so 2+1+2+2 = 7 is perfect number.
If Cat can't help Salmon, Salmon will be sad and Cat will be much more sad. So Cat must solve the problem to maintain their friendship. Can you help Cat to solve the problem?
Input
This problem contains multiple test cases.
Each test case contains one line.
Each line contains an interger n, 1 <= n <= 10^9.
Output
For each test case, if n is a perfect number, output “Yes”, otherwise output “No”.
Sample Input
3 7 8
Sample Output
Yes Yes No
Hint
1 /* 2 * this code is made by HaibaraAi 3 * Problem: 1115 4 * Verdict: Accepted 5 * Submission Date: 2014-06-27 22:42:38 6 * Time: 36 MS 7 * Memory: 2260 KB 8 */ 9 #include <cstdio> 10 using namespace std; 11 int n; 12 int a[300005]; 13 int dfs(int x){ 14 if (x == 1 || x == 3)return 1; 15 x += 2; 16 for (int i = 3; i*i <= x&&i<=5; i++) 17 if (x % i == 0 && x / i - 2>0) 18 if (dfs(i - 2) && dfs(x / i - 2))return 1; 19 return 0; 20 } 21 int main(){ 22 while (scanf("%d", &n)!=EOF){ 23 if (dfs(n))printf("Yes\n"); 24 else printf("No\n"); 25 } 26 return 0; 27 }
别人告知a*b+2*a+2*b+2=(a+2)(b+2)-2
结果分解dfs分解因式TLE。。。
后来别人又提示用3和5分解。。。
因为结果只可能由(a+2)...(b+2)...-2得来。。。用原式带入能看出来。。而且+2把带入的数从(a+2)..(b+2)-2又变成了(a+2)..(b+2)
太弱了。。。
浙公网安备 33010602011771号