教室编号
总提交 : 106 测试通过 : 7
描述
西南民族大学修建了一栋新的教学楼, 现在要给这个楼的一层教室编号(要求任意两个教室编号互不相同)。左侧的教室有num1个,要求左侧所有教室编号不被x整除。 右侧的教室有num2个,要求右侧所有教室编号不被y整除。校领导想让左右两侧所有教室编号的最大值尽量小,他请到你来帮忙规划一下。
输入
本题为多组数据评测。
第一行为一个整数T(1≤T ≤ 1000),表示数据组数。
对于每组测试数据:
仅一行,包含四个整数,num1,num2,x, y (1 ≤num1, num2 ≤ 109; 1 ≤ num1 + num2≤ 109 ; 2 ≤ x, y ≤ 30000,保证x,y均为质数)。含义如题所述。
输出
对于每组测试数据,输出一行"Case #x: y",其中x为数据组数, y为最大编号的最小值。
样例输入
2
3 1 2 3
1 3 2 3
样例输出
Case #1: 5
Case #2: 4
代码:
/*
_...---.._
,' ~~"--..
/ ~"-._
/ ~-.
/ . `-.
\ -.\ `-.
\ ~-. `-.
,-~~\ ~.
/ \ `.
. \ `.
| \ .
| \ \
. `. \
\ \
` `. \
` \. \
` \`. \
. \ -. \
` -. \
. ` - \ .
` \ ~- \
` . ~. \
. \ -_ \
` - \
. | ~. \
` | \ \
. | \ \
` | `. \
` ` \ \
. . `. `.
` : \ `.
\ ` \ `.
\ . `. `~~-.
\ : ` \ \
. . \ : `.\
` : \ | | .
\ . \ | |
\ : \ ` | `
. . | |_ .
` `. ` ` | ~.;
\ `. . . .
. `. ` ` `
`. `._. \ `.\
` < \ `. | .
` ` : ` | |
` \ ` | |
`. | \ : .' |
"Are you crying? " ` | \ `_-' |
"It's only the rain." : | | | : ;
"The rain already stopped."` ; |~-.| : '
"Devils never cry." : \ | ` ,
` \` : '
: \` `_/
` .\ "For we have none. Our enemy shall fall."
` ` \ "As we apprise. To claim our fate."
\ | : "Now and forever. "
\ .' : "We'll be together."
: : "In love and in hate"
| .'
| : "They will see. We'll fight until eternity. "
| ' "Come with me.We'll stand and fight together."
| / "Through our strength We'll make a better day. "
`_.' "Tomorrow we shall never surrender."
sao xin*/
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <cstdio>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <ctype.h>
#include<functional>
#define INF 0xfffffff
#define eps 1e-6
#define LL long long
using namespace std;
const int maxn=1e3+5;
const int maxx=1e4+5;
const double q = (1 + sqrt(5.0)) / 2.0; // 黄金分割数
/*
std::hex <<16进制 cin>>std::hex>>a>>std::hex>>b
cout.setf(ios::uppercase);cout<<std::hex<<c<<endl;
b=b>>1; 除以2 二进制运算
//f[i]=(i-1)*(f[i-1]+f[i-2]); 错排
/ for (int i=1; i<=N; i++)
for (int j=M; j>=1; j--)
{
if (weight[i]<=j)
{
f[j]=max(f[j],f[j-weight[i]]+value[i]);
}
}
priority_queue<int,vector<int>,greater<int> >que3;//注意“>>”会被认为错误,
priority_queue<int,vector<int>,less<int> >que4;最大值优先
//str
//tmp
//vis
//val
//cnt 2486*/
LL num1,num2;
LL x,y;
int cas=1;
int ok(LL mid)
{
int cnl;
if(x==y)
cnl=x;
else
cnl=x*y;
LL sum1,sum2;
sum1=mid/y-mid/cnl;
sum2=mid/x-mid/cnl;
LL tmp1=num1;
LL tmp2=num2;
tmp1-=sum1;
tmp2-=sum2;
tmp1=max(0ll,tmp1);
tmp2=max(0ll,tmp2);
mid-=mid/y+mid/x-mid/cnl;
if(mid>=tmp1+tmp2)
return 1;
else return 0;
}
int main()
{
int n;
cin>>n;
while(n--)
{
cin>>num1>>num2>>x>>y;
LL l=1;
LL r=(num1+num2)<<1;
while(l<r)
{
LL mid = (l+r) >> 1;
if (ok(mid)) r = mid;
else l = mid + 1;
}
printf("Case #%d: %lld\n", cas++, l);
}
return 0;
}
二分与1比较,如果成立继续二分,不成立则+1再判断。
首先先两个数加起来*2作为第一项,mid=(1+r)/2;
判断此时mid是否在范围内,如果是,再另r=mid,如果不是再另l=mid+1;