二分+容斥

Problem A: qwb与支教

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 1407   Solved: 241
[ Submit][ Status][ Web Board]

Description

qwb同时也是是之江学院的志愿者,暑期要前往周边地区支教,为了提高小学生的数学水平。她把小学生排成一排,从左至右从1开始依次往上报数。

玩完一轮后,他发现这个游戏太简单了。于是他选了3个不同的数x,y,z;从1依次往上开始报数,遇到x的倍数、y的倍数或z的倍数就跳过。如果x=2,y=3,z=5;第一名小学生报1,第2名得跳过2、3、4、5、6,报7;第3名得跳过8、9、10,报11。

那么问题来了,请你来计算,第N名学生报的数字是多少?

Input

多组测试数据,处理到文件结束。(测试数据数量<=8000)

每个测试例一行,每行有四个整数x,y,z,N。( 2≤x,y,z≤107,1≤N≤1017)。

Output

对于每个测试例,输出第N名学生所报的数字,每个报数占一行。

Sample Input

2 3 5 2
6 2 4 10000

Sample Output

7
19999

题意:给出三个数,然后找第几个不能整除他们的数;

思路:容斥+二分

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
const int maxn = 1000000 + 10;
#define INF 0x3f3f3f3f
#define clr(x,y) memset(x,y,sizeof x )
typedef long long ll;
#define eps 10e-10
const ll Mod = 1e9 + 7;
typedef pair<ll, ll> P;
ll gcd(ll x,ll y)
{
    return y ? gcd(y,x % y) : x;
}
ll pow_mod(ll x,ll n,ll mod_val)
{
    ll ans = 1;
    ll t = x % mod_val;
    while(n)
    {
        if(n & 1)
        {
            ans = ans * t % mod_val;
        }
        n >>= 1;
        t = t * t % mod_val;
    }
    return ans;
}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y)
{
    if(b == 0)
    {
        d = a;x = 1;y = 0;
        return ;
    }
    ex_gcd(b,a % b,d,y,x);
    y -= a / b * x;
}
ll a[5];
ll len;
ll x,y,z,n;
ll Eculsion(ll n)
{
    ll lcm1 = x /gcd(x,y) * y;
    ll lcm2 = x /gcd(x,z) * z;
    ll lcm3 = y / gcd(y,z) * z;
    ll lcm = lcm1 /gcd(lcm1,z) * z;
    return n - (n/x + n/y + n/z) +(n/lcm1 + n/lcm2 + n/lcm3) - n/lcm;
}
int main()
{
    while( ~ scanf("%lld%lld%lld%lld",&x,&y,&z,&n))
    {
        ll  l = 1,r = 2*1e17;
        while((r - l) > 0)
        {
            ll mid = (l + r) / 2;
 
            if(n <= Eculsion(mid))
            {
//                cout <<l << " " << r << " " << mid << " "<< Eculsion(mid) << endl;
                r = mid;
            }
            else l = mid + 1;
        }
        printf("%lld\n",l);
    }
    return 0;
}
 
/**************************************************************
    Problem: 1481
    User: ZJC2017Final110
    Language: C++
    Result: Accepted
    Time:224 ms
    Memory:1712 kb
****************************************************************/


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值