hdu 1576
2 1000 53 87 123456789Sample Output
7922 6060
#include
using namespace std;
#define LL long long
#define INF 1E4 * 1E9
#define pi acos(-1)
#define endl '\n'
#define me(x) memset(x,0,sizeof(x));
const int maxn=1e3+5;
const int maxx=1e5+5;
LL e_gcd(LL a,LL b,LL &x,LL &y)
{
if(b==0)
{
x=1;
y=0;
return a;
}
LL ans=e_gcd(b,a%b,x,y);
LL temp=x;
x=y;
y=temp-a/b*y;
return ans;
}
LL cal(LL a,LL b,LL c)
{
LL x,y;
LL gcd=e_gcd(a,b,x,y);
if(c%gcd!=0) return -1;
x*=c/gcd;//ax+by=c;
b/=gcd;
if(b<0) b=-b;
LL ans=x%b;//x0
if(ans<=0) ans+=b;//x通解 x0+b*t 最小的一个x
return ans;
}
int main()
{
LL n,b,t;
cin>>t;
while(t--)
{
cin>>n>>b;
LL k=cal(b,9973,1);
LL ans=(n*k)%(9973);
cout<
poj 2142
700 300 200 500 200 300 500 200 500 275 110 330 275 110 385 648 375 4002 3 1 10000 0 0 0Sample Output
1 3 1 1 1 0 0 3 1 1 49 74 3333 1
#include
#include
using namespace std;
void exgcd(int a,int b,int& g,int& x,int& y){ //int& a 是定义一个存放整形变量a的地址
if(!b){ g = a ; x = 1 ; y = 0;} // g用来存储gcd(a,b)的值
else { exgcd(b , a%b , g , y , x) ; y -= x * (a/b) ; }
}
void work(int a , int b , int d ,int& g , int& x , int& y){
exgcd(a,b,g,x,y); //此处的x,y接收 ax+by=gcd(a,b)的值
x *= d/g; //求ax+by=c 的解x
// y *= d/g; //求ax+by=c 的解y
int t = b/g;
x = (x%t + t) % t; //求出最小非负整数
y = abs( (a*x - d) / b); //求相对应的y,取绝对值是为了当左边砝码数 x 为零的时候,得出来的 y 是正整数。
/* //以下是先求y再求对应的x 。
int t = a/g;
y = (y%t + t) % t;
x = abs( (d + b*y) / a);
*/
}
int main(){
int a,b,d,g,x1,x2,y1,y2;
while(cin>>a>>b>>d){
if(a==0&&b==0&&d==0)break;
work(a,b,d,g,x1,y1);
work(b,a,d,g,x2,y2);
if( x1+y1 < x2+y2 )
cout<