poj 2142 && 2042

题目:http://poj.org/problem?id=2142

给出 a b d,找出 x 和 y 能够组合出 d,也就是满足 a * x + b * y = d,a 和 b 不一定是在天枰的同一边,利用扩展欧几里得求出 x , y, x,y可能为负值,把 x y 转化为最小的正整数,然后利用 最小的 x ,根据 ty = (d - a * x) / b,和最小的 y 根据 tx = (d - b * y) / a,判断 x + ty 和 y + tx的大小,然后输出结果

View Code
 1 typedef long long ll;
 2 int exgcd(int a,int b,int &x,int &y)
 3 {
 4     if(!b)
 5     {
 6         x = 1,y = 0;
 7         return a;
 8     }
 9     int d = exgcd(b,a % b,x,y);
10     int temp = x;
11     x = y;
12     y = temp - a / b * y;
13     return d;
14 }
15 int main()
16 {
17     int a,b,d;
18     int tem;
19     int x,y;
20     while(~scanf("%d%d%d",&a,&b,&d))
21     {
22         if(!a && !b && !d) break;
23         tem = exgcd(a,b,x,y);
24         x = (x * d) / tem;  //把 x y 转化为最小的正整数
25         y = (y * d) / tem;  
26         a /= tem; b /= tem; d /= tem;
27         x = (x % b + b) % b;
28         y = (y % a + a) % a;
29         int ty = (d - a * x) / b;
30         if(ty < 0) ty = -ty;
31         int tx = (d - y * b) / a;
32         if(tx < 0) tx = -tx;
33         if(x + ty > tx + y)
34         {
35             x = tx;
36             ty = y;
37         }
38         cout<<x<<" "<<ty<<endl;
39     }
40     return 0;
41 }

题目:http://poj.org/problem?id=2042

给出一个x 找出 sum(pi ^ 2) = x,i 最多为 4 个,因为 n 的范围是 2 ^ 15,所以直接打表算出每一个 x 符合条件的个数,然后对应输出即可

View Code
 1 typedef long long ll;
 2 const int N = 32768;
 3 int vis[32769];
 4 int main()
 5 {
 6     int i,j,k,h;
 7     int c[182];
 8     int n = 181;
 9     for(i = 1; i <= n; i++)
10     {
11         c[i] = i * i;
12     }
13     for(i = 1; i <= n; i++)
14     {
15         vis[c[i]] ++;
16     }
17     for(i = 1; i <= n; i++)
18     {
19         for(j = i; j <= n && c[i] + c[j] <= N; j++)
20         vis[c[i] + c[j]] ++;
21     }
22     for(i = 1; i <= n; i++)
23     {
24         for(j = i; j <= n && c[i] + c[j] <= N; j++)
25         {
26             for(k = j; k <= n && c[i] + c[j] + c[k] <= N; k++)
27             vis[c[i] + c[j] + c[k]] ++;
28         }
29     }
30     for(i = 1; i <= n; i++)
31     {
32         for(j = i; j <= n && c[i] + c[j] <= N; j++)
33         {
34             for(k = j; k <= n && c[i] + c[j] + c[k] <= N; k++)
35             {
36                 for(h = k; h <= n && c[i] + c[j] + c[k] + c[h] <= N; h++)
37                 vis[c[i] + c[j] + c[k] + c[h]] ++;
38             }
39         }
40     }
41     int m;
42     while(~scanf("%d",&m),m)
43     {
44         printf("%d\n",vis[m]);
45     }
46     return 0;
47 }
posted @ 2012-11-15 10:35  AC_Girl  阅读(224)  评论(0编辑  收藏  举报