QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#580980#9376. GamejiejiejiangML 0ms3704kbC++201.9kb2024-09-22 02:33:172024-09-22 02:33:17

Judging History

你现在查看的是最新测评结果

  • [2024-09-22 02:33:17]
  • 评测
  • 测评结果:ML
  • 用时:0ms
  • 内存:3704kb
  • [2024-09-22 02:33:17]
  • 提交

answer

//
// Created by zhouw on 24-9-21.
//
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MOD = 998244353;

const int N = 1e8;
int fact[N],infact[N];

int qpow(int a,int b)
{
    int res=1;
    for(;b;b/=2,a=1ll * a * a % MOD)
    {
        if(b % 2)
            res = 1ll * res * a % MOD;
    }
    return res;
}

void ini()
{
    fact[0] = infact[0] = 1;
    for(int i = 1 ; i < N ; i ++)
    {
        fact[i] = fact[i-1] * i % MOD;
        infact[i] = qpow(fact[i],MOD-2);
    }
}

int C(int a,int b)
{
    if(a < b) return 0;
    return fact[a] * infact[b] % MOD * infact[a-b] % MOD;
}

std::pair<int,int> win(int x,int y,int a0,int a1)
{
    int num;
    num = 1 + (y-x)/x + ((y-x)%x !=0);
    if(x >= y) num = 1;
    int zi = qpow(a0,num)%MOD;

    int y1 = y % x , x1 = x - y1;
    std::pair<int,int> res;
    if(x1 >= 1 && y1) res = win(x1,y1,a0,a1);
    else if(y1 == 1)
    {
        int in = 0;
        for(int i = 0 ; i <= num ; i ++)
        {
            in = (in + (qpow(a1,i)*qpow(a0+a1,num-i))%MOD)%MOD;
        }
        res = {x1 - y1,(in*a0)%MOD};
    }
    else res = {0,0};

    int zi1 = (qpow(a0,num-1)*a1)%MOD;

    num = num + res.first;
    zi1 = (zi1*res.second)%MOD;
    zi = (zi*qpow(a0+a1,res.first))%MOD;

    //std::cout << x << " " << y << " " << num << " " << (zi+zi1)%MOD << "\n";
    return {num,(zi+zi1)%MOD} ;
}

signed main()
{
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);

    int t;
    cin>>t;
    while(t--)
    {
        int x,y;
        cin>>x>>y;
        int a0,a1,b;
        cin>>a0>>a1>>b;
        std::pair<int,int> www = win(x,y,a0,a1);
        int mu = qpow(a0+a1,www.first);
        int ans = (www.second*qpow(mu,MOD-2))%MOD;
        //std::cout << mu << " " << www.second << "\n";
        std::cout << ans << "\n";
    }
    //std::cout << MOD-2 << "\n";
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3704kb

input:

3
1 1
2 2 6
1 3
2 3 6
3 4
7 3 15

output:

499122177
910398850
220911476

result:

ok 3 lines

Test #2:

score: -100
Memory Limit Exceeded

input:

100000
1 1000000000
12980050 128257807 266126484
1 1000000000
400255084 123438563 768881284
1000000000 1000000000
24563487 72082135 450057094
1 1000000000
56952077 40876000 193815114
1000000000 1000000000
82048274 239365585 326520865
1000000000 1
309821265 346013425 963168258
1 1
104158269 199365020...

output:


result: