其他分享
首页 > 其他分享> > E. Calendar Ambiguity

E. Calendar Ambiguity

作者:互联网

E. Calendar Ambiguity

链接

题目大意

给定\(m,d,w\),表示一年中有\(m\)个月,每个月有\(d\)天,一周有\(w\)天,现在求出\((x,y)\)的对数,其满足:
\(x < y\)并且\(x\)月\(y\)日和\(y\)月\(x\)日是一周中的同一天。

解题思路

显然是要求这个式子:

\[[(i - 1) * d + j] - [(j - 1) * d + i] = 0 (mod\ w) \]

即: \((d-1)(i- j) = 0 (mod\ w)\),(i > j)
因为\(d,w\)已知,所以求出\(g = gcd(d - 1, w), d - 1 = k_1 * g, w = k_2 * g\)
则此时有: \(k_1 * g * (i - j) = k * k_2 * g => (i - j) = \cfrac{k}{k_1} * \cfrac{w}{g}\),因为\((i-j),\cfrac{w}{g}\)是整数,所以\(\cfrac{k}{k_1}\)也是整数,所以\((i - j)\)是\(\cfrac{w}{g}\)的倍数。
取\(w' = \cfrac{w}{g}\)
因此我们可以想到枚举 \(i\):

Code

#include <bits/stdc++.h>
#define ll long long
#define qc ios::sync_with_stdio(false); cin.tie(0);cout.tie(0)
#define fi first
#define se second
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define pb push_back
#define V vector
using namespace std;
const int N = 2e5 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
    while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
    return x*f;
}

ll m,d,w;
ll gcd(ll x,ll y)
{
    return y ? gcd(y, x % y) : x;
}
void solve(){
    scanf("%lld%lld%lld",&m,&d,&w);
    ll g = gcd(d - 1, w);
    m = min(m, d);
    ll w1 = w / g;
    ll p = (m - 1) / w1;
    ll ans = ((m - 1) % w1 + 1) * p + w1 * ((p - 1) * p / 2);
    printf("%lld\n",ans);
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif

    int T = 1;
    scanf("%d",&T);
    while(T--){
        solve();
    }
    return 0;
}

标签:ch,const,int,ll,cfrac,Ambiguity,Calendar,define
来源: https://www.cnblogs.com/KeepInCode/p/15442882.html