教室编号

教室编号

标签:  二分查找  
2016-12-05 08:09阅读(4) 评论(0) 编辑 删除

教室编号

时间限制(普通/Java) : 1000 MS/ 3000 MS          运行内存限制 : 65536 KByte
总提交 : 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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值