ARC123E - Training

题意见这篇博客
考虑 \(f(x) = a + \lfloor \frac{x}{b} \rfloor\) \(g(x)= c + \lfloor \frac{x}{d} \rfloor\) 这两个函数相等的条件。
不难发现如果 \(f(x) = g(x)\) (\(f'(x) = a + \frac{x}{b}\) \(g'(x) = c + \frac{x}{d}\)) 那么 \(\vert f'(x) - g'(x) \vert \leq 1\)
显然这个区间会分为两个区间 满足区间内 \(f'(x) >= g'(x)\)\(f'(x) < g'(x)\) 不失一般性的,我们只考虑前者。
那么答案就是区间长度 \(- \Sigma f'(x) - g'(x)\) 因为如果这两个数相差为 \(1\) 就会体现在 \(RSH\) 中,反之则体现在剩下之中,本题就解决了。

Tips:
取整符号如果不好处理那就可以去掉然后考虑。
有必要考虑答案是否在一个连续区间内。

#include<bits/stdc++.h>
#define RG register
#define LL long long
#define U(x, y, z) for(RG int x = y; x <= z; ++x)
#define D(x, y, z) for(RG int x = y; x >= z; --x)
#define update(x, y) (x = x + y >= mod ? x + y - mod : x + y)
using namespace std;
const int mod = 998244353;
namespace FastIO {
#define il inline
const int iL = 1 << 25;
char ibuf[iL], *iS = ibuf + iL, *iT = ibuf + iL;
#define GC() (iS == iT) ? \
  (iT = (iS = ibuf) + fread(ibuf, 1, iL, stdin), (iS == iT) ? EOF : *iS++) : *iS++
void read(){}
template<typename _Tp, typename... _Tps>
void read(_Tp &x, _Tps &...Ar) {
    x = 0; char ch = GC(); bool flg = 0;
    for (; !isdigit(ch); ch = GC()) flg |= (ch == '-');
    for (; isdigit(ch); ch = GC()) x = (x << 1) + (x << 3) + (ch ^ 48);
    if (flg) x = -x;
    read(Ar...);    
}
char Out[iL], *iter = Out;
#define Flush() fwrite(Out, 1, iter - Out, stdout); iter = Out
template <class T>il void write(T x, char LastChar = '\n') {
    int c[35], len = 0;
    if (x < 0) {*iter++ = '-'; x = -x;}
    do {c[++len] = x % 10; x /= 10;} while (x);
    while (len) *iter++ = c[len--] + '0';
    *iter++ = LastChar; Flush();
}
template <typename T> inline void writeln(T n){write(n, '\n');}
template <typename T> inline void writesp(T n){write(n, ' ');}
inline char Getchar(){ char ch; for (ch = GC(); !isalpha(ch); ch = GC()); return ch;}
inline void readstr(string &s) { s = ""; static char c = GC(); while (isspace(c)) c = GC(); while (!isspace(c)) s = s + c, c = GC();}
}
using namespace FastIO;
struct modint{
    int x;
    modint(int o=0){x=o;}
    modint &operator = (int o){return x=o,*this;}
    modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
    modint &operator -=(modint o){return x=x-o.x<0?x-o.x+mod:x-o.x,*this;}
    modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
    modint &operator ^=(int b){
        if(b<0)return x=0,*this;
        b%=mod-1;
        modint a=*this,c=1;
        for(;b;b>>=1,a*=a)if(b&1)c*=a;
        return x=c.x,*this;
    }
    modint &operator /=(modint o){return *this *=o^=mod-2;}
    modint &operator +=(int o){return x=x+o>=mod?x+o-mod:x+o,*this;}
    modint &operator -=(int o){return x=x-o<0?x-o+mod:x-o,*this;}
    modint &operator *=(int o){return x=1ll*x*o%mod,*this;}
    modint &operator /=(int o){return *this *= ((modint(o))^=mod-2);}
    template<class I>friend modint operator +(modint a,I b){return a+=b;}
    template<class I>friend modint operator -(modint a,I b){return a-=b;}
    template<class I>friend modint operator *(modint a,I b){return a*=b;}
    template<class I>friend modint operator /(modint a,I b){return a/=b;}
    friend modint operator ^(modint a,int b){return a^=b;}
    friend bool operator ==(modint a,int b){return a.x==b;}
    friend bool operator !=(modint a,int b){return a.x!=b;}
    bool operator ! () {return !x;}
    modint operator - () {return x?mod-x:0;}
};
template <typename T> inline void chkmin(T &x, T y){x = x < y ? x : y;}
template <typename T> inline void chkmax(T &x, T y){x = x > y ? x : y;}
template <typename T> inline T Min(T x, T y){return x < y ? x : y;}
template <typename T> inline T Max(T x, T y){return x > y ? x : y;}
inline void FO(string s){freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout);}

LL sum(LL a, LL b) {
	LL n = a / b;
	return n * (n - 1) / 2 * b + (a - b * n) * n;
}
LL calc(LL l, LL r, LL a, LL b) {
	return a * (r - l) + sum(r, b) - sum(l, b);
}

inline void solve() {
	LL n, a, b, c, d, l, r, ans = 0;
	read(n, a, b, c, d);
	// cerr << b << " " << d << "\n";
	if (b == d) {
		writeln(a == c ? n : 0);
		return ;
	}
	if (b > d) swap(a, c), swap(b, d);
	l = max(0ll, b * d * (c - a - 1) / (d - b)) + 1;
	r = min(n, b * d * (c - a) / (d - b)) + 1;
	if(l < r) ans = r - l - calc(l, r, c, d) + calc(l, r, a, b);
	l = max(0ll, b * d * (c - a) / (d - b)) + 1;
	r = min(n, b * d * (c - a + 1) / (d - b)) + 1;
	if(l < r) ans += r - l - calc(l, r, a, b) + calc(l, r, c, d);
	// cerr << l << " " << r << " " << ans << "\n";
	writeln(ans);
}

int main(){
	// FO("ARC123E");
	int T;
	read(T);
	while (T--) solve();
	return 0;
}
posted @ 2022-11-24 14:55  Southern_Way  阅读(26)  评论(0)    收藏  举报