hdu 1576
要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1)。
每组数据有两个数n(0 <= n < 9973)和B(1 <= B <= 10^9)。
2 1000 53 87 123456789
7922 6060
#include <bits/stdc++.h>
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<<ans<<endl;
}
}
poj 2142
Ms. Iyo Kiffa-Australis has a balance and only two kinds of weights to measure a dose of medicine. For example, to measure 200mg of aspirin using 300mg weights and 700mg weights, she can put one 700mg weight on the side of the medicine and three 300mg weights on the opposite side (Figure 1). Although she could put four 300mg weights on the medicine side and two 700mg weights on the other (Figure 2), she would not choose this solution because it is less convenient to use more weights.
You are asked to help her by calculating how many weights are required.
You are asked to help her by calculating how many weights are required.
The input is a sequence of datasets. A dataset is a line containing three positive integers a, b, and d separated by a space. The following relations hold: a != b, a <= 10000, b <= 10000, and d <= 50000. You may assume that it is possible to measure d mg using a combination of a mg and b mg weights. In other words, you need not consider "no solution" cases.
The end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset.
The end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset.
The output should be composed of lines, each corresponding to an input dataset (a, b, d). An output line should contain two nonnegative integers x and y separated by a space. They should satisfy the following three conditions.
No extra characters (e.g. extra spaces) should appear in the output.
- You can measure dmg using x many amg weights and y many bmg weights.
- The total number of weights (x + y) is the smallest among those pairs of nonnegative integers satisfying the previous condition.
- The total mass of weights (ax + by) is the smallest among those pairs of nonnegative integers satisfying the previous two conditions.
No extra characters (e.g. extra spaces) should appear in the output.
700 300 200 500 200 300 500 200 500 275 110 330 275 110 385 648 375 4002 3 1 10000 0 0 0
1 3 1 1 1 0 0 3 1 1 49 74 3333 1
#include<iostream>
#include<cmath>
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<<x1<<" "<<y1<<endl;
else
cout<<y2<<" "<<x2<<endl; //(注意顺序)
}
return 0;
}