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\)

  • \(i = m\),这时\(1 \leq j \leq m -1\)\(1 \leq (i-j) \leq m-1\),所以贡献为\(\cfrac{m-1}{w'}\)
  • \(\cdots\)
    因此答案为\(\cfrac{m-1}{w'} + \cfrac{m-2}{w'} + \cdots + \cfrac{1}{w}\),
    因为是向下取整所以不可以合并计算。
    我们发现这个答案一定是\(1 + 1 + \cdots + k\)
    并且根据除法的性质可以知道除了最后一个\(k\),所有的数都出现了\(w'\)次,而最后一个数出现了\((m - 1)\ \%\ w' + 1\)次,令\(x = \cfrac{m - 1}{w'}\)
    则答案就是\(x * ((m - 1)\ \%\ w + 1) + w' * ((x - 1) * x\ /\ 2)\)

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;
}
posted @ 2021-10-23 12:14  !^^!  阅读(45)  评论(0编辑  收藏  举报