ZOJ - 4056(2018 青岛网络赛J) - LCM

版权声明:欢迎随便转载。 https://blog.csdn.net/a1214034447/article/details/82749553

题目链接:点击这里

解题思路:

将t变为最近一个a或者c的倍数,这样可以避免在最后的时候有可能的坑.

然后很容易就想到最小公倍数是一个周期,然后最后在跑一个求余的长度.

时间复杂度O(T*max(a,c))

#include<bits/stdc++.h>
#define lson l,mid
#define rson mid+1,r
using namespace std;
typedef long long ll;
int n,m;
const int mx = 1e5+10;
ll a,b,c,d,v,t;
ll cal(ll x)
{
	ll pre = 0,ans = 0;
	ll at = 0,ct = 0,s;
	while(pre<x){
		s = min(at+a,ct+c);
		s = min(x,s);
		if(s-pre>=v) ans++;
		if(at+a<=ct+c) at += a;
		else ct += c;
		pre = s;
	}
	return ans;
}
int main()
{
    int tt,cas = 1;
    scanf("%d",&tt);
    while(tt--){
    	scanf("%lld%lld%lld%lld%lld%lld",&a,&b,&c,&d,&v,&t);
    	v++;
    	t = max(t/a*a,t/c*c);
    	ll lcm = a/__gcd(a,c)*c;
    	ll ans = (t/a+1)*b + (t/c+1)*d - 1;
    	ans -= cal(lcm)*(t/lcm);
    	ans -= cal(t%lcm);
    	printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/a1214034447/article/details/82749553
今日推荐