poj The Luckiest number

                                     The Luckiest numbe

题目:

在满足|x| + |y|最小时候,让a*|x| + b*|y|最小。求:|x| + |y|最小值。

算法:

同余式的求解运用。

解体步骤:

1、先用gcd判断是否有解。(c%g == 0有解)

2、对式子求最简式。a‘ = a/g ; b‘ = b/g; c‘ = c/g;

3、运用扩展欧几里得求解x,y的值。

4、判断当x,y分别求得最小正整数解时候的大小。

5、确定最后答案。

处理过程中有一些细节:

1、对求的x,y并不是正确的x,y值。要x = x * c; y = y * c

2、保证x或y是正数.(x%b + b)%b \ (y%a + a)%a

/*
    1、|x| + |y|最小
    2、在1相同的时候a*|x| + b*|y|最小
*/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

typedef long long LL;

LL gcd(LL a,LL b){
   return b?gcd(b,a%b):a;
}

void extgcd(LL a,LL b,LL& d,LL& x,LL& y){
    if(!b) { d = a; x = 1; y = 0; }
    else { extgcd(b,a%b,d,y,x); y -= x * (a/b); }
}

int main()
{
    LL a,b,c;
    while(cin >> a >> b >> c){
        if(a == 0&&b == 0&&c == 0)break;
        LL d = gcd(a,b);

        a /= d; b /= d; c /= d;
        LL x,y;
        extgcd(a,b,d,x,y);

        LL x1,y1,x2,y2;

        //y 取得最小正整数
        y1 = y * c;
        y1 = (y1 % a + a) % a;
        x1 = (c - b*y1) / a;
        if(x1 < 0) x1 = -x1;  //砝码个数非负

        //x取得最小正整数
        x2 = x * c;
        x2 = (x2 % b + b) % b;
        y2 = (c - a*x2) / b;
        if(y2 < 0) y2 = -y2;

        if(x1 + y1 < x2 + y2){x = x1; y = y1;}
        else {x = x2; y = y2;}

        cout << x << " " << y << endl;
    }
    return 0;
}

/*
700 300 200
500 200 300
500 200 500
275 110 330
275 110 385
648 375 4002
3 1 10000
0 0 0
*/
时间: 2024-10-11 00:17:13

poj The Luckiest number的相关文章

The Luckiest number(hdu2462)

The Luckiest number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1163    Accepted Submission(s): 363 Problem Description Chinese people think of '8' as the lucky digit. Bob also likes digit '

poj 2104 K-th Number(划分树模板)

划分树模板题,敲上模板就ok了. #include<algorithm> #include<iostream> #include<cstring> #include<vector> #include<cstdio> #include<cmath> #include<queue> #include<stack> #include<map> #include<set> #define MP

【POJ 1019】 Number Sequence

[POJ 1019] Number Sequence 二分水题 放组合数学里...可能有什么正规姿势吧Orz 112123123412345...这种串 分成长度1 2 3 4 5...的串 注意有多位数 把长度累加到一个数组里 注意要累加 因为查询的时候查的是原串中对应位置的数 因此要累加上前一次的长度 然后二分处该串前的总长 用查询的位置-之前串的总长 就是在最长的串中的位置 因此还要打个最长串的表 这些我都写一个循环里了 看着有点乱 可以拆开写... 代码如下: #include <ios

poj_3696_The Luckiest number

Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his own lucky number L. Now he wants to construct his luckiest number which is the minimum among all positive integers that are a multiple of L and consist of

POJ_3696 The Luckiest number 【欧拉定理+同余式+对取模的理解】

一.题目 Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his own lucky number L. Now he wants to construct his luckiest number which is the minimum among all positive integers that are a multiple of L and consi

POJ3696 The Luckiest Number

Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his own lucky number L. Now he wants to construct his luckiest number which is the minimum among all positive integers that are a multiple of L and consist of

poj 3696 The Luckiest number 欧拉函数在解a^x=1modm的应用

题意: 给一个L,求长度最小的全8数满足该数是L的倍数. 分析: 转化为求方程a^x==1modm.之后就是各种数学论证了. 代码: //poj 3696 //sep9 #include <iostream> #include <algorithm> using namespace std; typedef long long ll; ll L; ll factor[65536]; ll mul(ll x,ll y,ll p) { ll ret=0; while(y){ if(y&

POJ 2329 -- Nearest number - 2

Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4224   Accepted: 1308 Description Input is the matrix A of N by N non-negative integers. A distance between two elements Aij and Apq is defined as |i ? p| + |j ? q|. Your program must repla

[划分树] POJ 2104 K-th Number

K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 51732   Accepted: 17722 Case Time Limit: 2000MS Description You are working for Macrohard company in data structures department. After failing your previous task about key inse