結果

問題 No.2280 FizzBuzz Difference
ユーザー lddlinanlddlinan
提出日時 2023-04-22 02:31:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 2,064 bytes
コンパイル時間 751 ms
コンパイル使用メモリ 84,536 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-24 11:18:19
合計ジャッジ時間 1,327 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 9 ms
5,376 KB
testcase_02 AC 10 ms
5,376 KB
testcase_03 AC 9 ms
5,376 KB
testcase_04 AC 11 ms
5,376 KB
testcase_05 AC 10 ms
5,376 KB
testcase_06 AC 10 ms
5,376 KB
testcase_07 AC 11 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <algorithm>
#include <array>
#include <unordered_set>
#include <unordered_map>
#include <string>
using namespace std;

bool rcmp(int a, int b) { return a>b; }
typedef long long LL;
class mypcmp {
public:
    bool operator()(const int& a, const int& b) {
        return a<b;
    }
};



long long egcd(long long a, long long b, long long *x, long long *y)
{
	if (a == 0) {
		*x = 0; *y = 1;
		return b;
	}
	long long  _x, _y;
	long long g = egcd(b % a, a, &_x, &_y);
	*x = _y - (b/a) * _x;
	*y = _x;
	return g;
}

int main() {
    int n, i;
    LL m, a, b, k, x, y, g, v, c, r, t, fa, fb, cx;
    int tc; scanf("%d", &tc); while(tc) { tc--;
        scanf("%lld %lld %lld %lld", &m, &a, &b, &k);
        g = egcd(a, b, &x, &y);
        v=a/g; v*=b; 
        if (k>a) printf("0\n");
        else if (k==a) {
            m -= m%a;
            r = m/a-m/b-1+m/v;
            printf("%lld\n", r);
        } else {
            if (k%g) printf("0\n");
            else {
                y=-y;
                // x*a-y*b = g
                x*=(k/g); y*=(k/g);
                // x*a-y*b = k
                fa=b/g; fb=a/g;
                cx=x/fa;
                x-=cx*fa;
                y-=cx*fb;
                while (x<=0) { x+=fa; y+=fb; }
                while (y<=0) { x+=fa; y+=fb; }
                r=m/a-x;
                if (r<0) r=0;
                else {
                    r = r/fa+1;
                }
                // printf("%lld*%lld-%lld*%lld=%lld --> %lld\n", x, a, y, b, k, r);
                x=-x; y=-y;
                while (x<=0) { x+=fa; y+=fb; }
                while (y<=0) { x+=fa; y+=fb; }
                t = m/b-y;
                if (t<0) t=0;
                else t = t/fb+1;
                // printf("%lld*%lld-%lld*%lld=-%lld --> %lld\n", x, a, y, b, k, t);
                r+=t;
                printf("%lld\n", r);
            }
        }
    }
    return 0;
}
0