QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#706660 | #273. 类欧几里得算法 | TheZone | 100 ✓ | 31ms | 3904kb | C++23 | 4.9kb | 2024-11-03 12:50:11 | 2024-11-03 12:50:12 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define FF first
#define SS second
#define PB push_back
#define MP make_pair
#define foreach(it,s) for(__typeof((s).begin()) it=(s).begin();it!=(s).end();it++)
#ifndef LOCAL
#define cerr if(0)cout
#endif
typedef long long ll;
const int mod = 1e9+7;
ll qpow(ll x, ll k) {return k==0? 1: 1ll*qpow(1ll*x*x%mod,k>>1)*(k&1?x:1)%mod;}
const int maxk = 11;
typedef unsigned long long ull;
int k1, k2;
int ifac[maxk], fac[maxk], s2[maxk][maxk];
struct node
{
int x[maxk], y[maxk], S[maxk][maxk];
node()
{
memset(x, 0, sizeof(x));
memset(y, 0, sizeof(y));
x[0] = y[0] = 1;
memset(S, 0, sizeof(S));
}
node operator + (const node &t)
{
node ret;
for (int i=0; i<=k1; i++)
{
ull sum = 0;
for (int j=0; j<=i; j++) sum += 1ll*x[j]*t.x[i-j];
ret.x[i] = sum%mod;
}
for (int i=0; i<=k2; i++)
{
ull sum = 0;
for (int j=0; j<=i; j++) sum += 1ll*y[j]*t.y[i-j];
ret.y[i] = sum%mod;
}
int tmp[maxk][maxk] = {};
for (int i=0; i<=k1; i++)
{
for (int j=0; j<=k2; j++)
{
ull sum = 0;
for (int k=0; k<=j; k++)
{
sum += 1ll*y[k]*t.S[i][j-k];
}
tmp[i][j] = sum%mod;
}
}
for (int i=0; i<=k1; i++)
{
for (int j=0; j<=k2; j++)
{
ull sum = 0;
for (int k=0; k<=i; k++)
{
sum += 1ll*x[k]*tmp[i-k][j];
}
ret.S[i][j] = (S[i][j]+sum)%mod;
}
}
return ret;
}
ll query()
{
ll ret = 0;
for (int i=0; i<=k1; i++) for (int j=0; j<=k2; j++)
{
ret = (ret+1ll*S[i][j]*s2[k1][i]%mod*s2[k2][j]%mod*fac[i]%mod*fac[j])%mod;
}
return ret;
}
}NU, NR;
node qsum(node x, int k) {return k==0?node():(k&1?qsum(x+x, k>>1)+x:qsum(x+x, k>>1));}
void prework()
{
fac[0] = 1;
for (int i=1; i<maxk; i++) fac[i] = 1ll*fac[i-1]*i%mod;
for (int i=0; i<maxk; i++) ifac[i] = qpow(fac[i], mod-2);
s2[0][0] = 1;
for (int i=1; i<maxk; i++) for (int j=1; j<=i; j++) s2[i][j] = (s2[i-1][j-1]+1ll*j*s2[i-1][j])%mod;
NR.x[0] = NR.x[1] = 1;
NR.y[0] = 1;
NR.S[0][0] = NR.S[1][0] = 1;
NU.x[0] = 1;
NU.y[0] = NU.y[1] = 1;
}
node solve(ll n, ll a, ll b, ll c, node A, node B)
{
assert(n>=0); assert(a>=0); assert(b>=0); assert(c>=0);
b %= c;
if (n==0) return node();
if (a>=c) return solve(n, a%c, b, c, A, qsum(A, a/c)+B);
ll m = (a*n+b)/c;
if (m==0) return qsum(B, n);
ll cnt = n-(m*c-b-1)/a;
return qsum(B, (c-b-1)/a)+A+solve(m-1, c, -b-1+c, a, B, A)+qsum(B, cnt);
}
int main()
{
prework();
int Tn;
cin>>Tn;
while (Tn--)
{
ll n, a, b, c;
cin>>n>>a>>b>>c>>k1>>k2;
node cur = qsum(NU, b/c)+solve(n, a, b, c, NU, NR);
ll ans = cur.query();
if (k1==0) ans = (ans+qpow(b/c, k2))%mod;
cout<<(ans+mod)%mod<<endl;
}
return 0;
}
/*#include<bits/stdc++.h>
using namespace std;
#define FF first
#define SS second
#define PB push_back
#define MP make_pair
#define foreach(it,s) for(__typeof((s).begin()) it=(s).begin();it!=(s).end();it++)
#ifndef LOCAL
#define cerr if(0)cout
#endif
typedef long long ll;
const int mod = 1e9+7;
ll qpow(ll x, ll k) {return k==0? 1: 1ll*qpow(1ll*x*x%mod,k>>1)*(k&1?x:1)%mod;}
const int maxk = 11;
typedef unsigned long long ull;
int k1, k2;
int ifac[maxk], fac[maxk], s2[maxk][maxk];
struct node
{
int x[maxk], y[maxk], S[maxk][maxk];
node()
{
memset(x, 0, sizeof(x));
memset(y, 0, sizeof(y));
x[0] = y[0] = 1;
memset(S, 0, sizeof(S));
}
node operator + (const node &t)
{
node ret;
for (int i=0; i<=k1; i++)
{
ull sum = 0;
for (int j=0; j<=i; j++) sum += 1ll*x[j]*t.x[i-j];
ret.x[i] = sum%mod;
}
for (int i=0; i<=k2; i++)
{
ull sum = 0;
for (int j=0; j<=i; j++) sum += 1ll*y[j]*t.y[i-j];
ret.y[i] = sum%mod;
}
int tmp[maxk][maxk] = {};
for (int i=0; i<=k1; i++)
{
for (int j=0; j<=k2; j++)
{
ull sum = 0;
for (int k=0; k<=j; k++)
{
sum += 1ll*y[k]*t.S[i][j-k];
}
tmp[i][j] = sum%mod;
}
}
for (int i=0; i<=k1; i++)
{
for (int j=0; j<=k2; j++)
{
ull sum = 0;
for (int k=0; k<=i; k++)
{
sum += 1ll*x[k]*tmp[i-k][j];
}
ret.S[i][j] = (S[i][j]+sum)%mod;
}
}
return ret;
}
ll query()
{
ll ret = 0;
for (int i=0; i<=k1; i++) for (int j=0; j<=k2; j++)
{
ret = (ret+1ll*S[i][j]*s2[k1][i]%mod*s2[k2][j]%mod*fac[i]%mod*fac[j])%mod;
}
return ret;
}
}NU, NR;
node qsum(node x, int k) {return k==0?node():(k&1?qsum(x+x, k>>1)+x:qsum(x+x, k>>1));}
void prework()
{
fac[0] = 1;
for (int i=1; i<maxk; i++) fac[i] = 1ll*fac[i-1]*i%mod;
for (int i=0; i<maxk; i++) ifac[i] = qpow(fac[i], mod-2);
s2[0][0] = 1;
for (int i=1; i<maxk; i++) for (int j=1; j<=i; j++) s2[i][j] = (s2[i-1][j-1]+1ll*j*s2[i-1][j])%mod;
NR.x[0] = NR.x[1] = 1;
NR.y[0] = 1;R);
ll ans = cur.query();
if (k1==0) ans = (ans+qpow(b/c, k2))%mod;
cout<<(ans+mod)%mod<<endl;
}
return 0;
}*/
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 10
Accepted
time: 14ms
memory: 3876kb
input:
1000 846930887 681692778 714636916 89384 0 1 424238336 719885387 649760493 47794 0 1 189641422 25202363 350490028 16650 0 1 102520060 44897764 967513927 68691 0 1 540383427 304089173 303455737 80541 0 1 521595369 294702568 726956430 5212 0 1 861021531 278722863 233665124 65783 0 1 468703136 10151393...
output:
787440837 603410377 723035859 327613252 213481743 197744321 183595532 306097937 945612263 462240557 878873337 913033603 276973800 137776104 471637127 36869524 59950373 599468074 662996688 39221965 159523453 603757410 863747292 125209174 321695224 581226543 502962761 546511215 492741651 881346590 834...
result:
ok 1000 numbers
Test #2:
score: 10
Accepted
time: 14ms
memory: 3904kb
input:
1000 846930887 681692778 714636916 89384 0 1 424238336 719885387 649760493 47794 0 1 189641422 25202363 350490028 16650 0 1 102520060 44897764 967513927 68691 0 1 540383427 304089173 303455737 80541 0 1 521595369 294702568 726956430 5212 0 1 861021531 278722863 233665124 65783 0 1 468703136 10151393...
output:
787440837 603410377 723035859 327613252 213481743 197744321 183595532 306097937 945612263 462240557 878873337 913033603 276973800 137776104 471637127 36869524 59950373 599468074 662996688 39221965 159523453 603757410 863747292 125209174 321695224 581226543 502962761 546511215 492741651 881346590 834...
result:
ok 1000 numbers
Test #3:
score: 10
Accepted
time: 15ms
memory: 3824kb
input:
1000 846930887 681692778 714636916 89384 1 0 649760493 596516650 189641422 85387 0 1 102520060 44897764 967513927 68691 0 0 303455737 35005212 521595369 89173 1 0 861021531 278722863 233665124 65783 1 0 801979803 315634023 635723059 13930 1 0 89018457 628175012 656478043 61394 1 0 914544920 60841378...
output:
590247101 607294734 102520061 988535616 258549494 359848706 860104659 914544921 806512744 219134560 36869524 54386320 1100547 760313752 603757410 510232691 82579690 843146721 36876088 935671592 290199337 365292116 534011850 126900199 669344073 690573152 719144156 644864030 602224207 100895714 452066...
result:
ok 1000 numbers
Test #4:
score: 10
Accepted
time: 11ms
memory: 3820kb
input:
1000 846930887 681692778 714636916 89384 1 0 649760493 596516650 189641422 85387 0 1 102520060 44897764 967513927 68691 0 0 303455737 35005212 521595369 89173 1 0 861021531 278722863 233665124 65783 1 0 801979803 315634023 635723059 13930 1 0 89018457 628175012 656478043 61394 1 0 914544920 60841378...
output:
590247101 607294734 102520061 988535616 258549494 359848706 860104659 914544921 806512744 219134560 36869524 54386320 1100547 760313752 603757410 510232691 82579690 843146721 36876088 935671592 290199337 365292116 534011850 126900199 669344073 690573152 719144156 644864030 602224207 100895714 452066...
result:
ok 1000 numbers
Test #5:
score: 10
Accepted
time: 30ms
memory: 3752kb
input:
1000 846930887 681692778 714636916 89384 3 3 649760493 596516650 189641422 85387 2 3 102520060 44897764 967513927 68691 0 6 303455737 35005212 521595369 89173 7 0 861021531 278722863 233665124 65783 7 1 801979803 315634023 635723059 13930 9 0 89018457 628175012 656478043 61394 9 0 914544920 60841378...
output:
269986411 687117872 337796106 649269006 273534477 925890819 789776059 781917067 471414212 683680813 655243026 766680733 110386800 920667633 42177293 33248798 268698025 97602241 455950431 787378605 628127823 884695308 910301084 481441390 301149571 40678494 744524425 997602040 853435603 942399367 4371...
result:
ok 1000 numbers
Test #6:
score: 10
Accepted
time: 30ms
memory: 3876kb
input:
1000 846930887 681692778 714636916 89384 3 3 649760493 596516650 189641422 85387 2 3 102520060 44897764 967513927 68691 0 6 303455737 35005212 521595369 89173 7 0 861021531 278722863 233665124 65783 7 1 801979803 315634023 635723059 13930 9 0 89018457 628175012 656478043 61394 9 0 914544920 60841378...
output:
269986411 687117872 337796106 649269006 273534477 925890819 789776059 781917067 471414212 683680813 655243026 766680733 110386800 920667633 42177293 33248798 268698025 97602241 455950431 787378605 628127823 884695308 910301084 481441390 301149571 40678494 744524425 997602040 853435603 942399367 4371...
result:
ok 1000 numbers
Test #7:
score: 10
Accepted
time: 26ms
memory: 3884kb
input:
1000 846930887 681692778 714636916 89384 3 3 649760493 596516650 189641422 85387 2 3 102520060 44897764 967513927 68691 0 6 303455737 35005212 521595369 89173 7 0 861021531 278722863 233665124 65783 7 1 801979803 315634023 635723059 13930 9 0 89018457 628175012 656478043 61394 9 0 914544920 60841378...
output:
269986411 687117872 337796106 649269006 273534477 925890819 789776059 781917067 471414212 683680813 655243026 766680733 110386800 920667633 42177293 33248798 268698025 97602241 455950431 787378605 628127823 884695308 910301084 481441390 301149571 40678494 744524425 997602040 853435603 942399367 4371...
result:
ok 1000 numbers
Test #8:
score: 10
Accepted
time: 30ms
memory: 3832kb
input:
1000 846930887 681692778 714636916 89384 3 3 649760493 596516650 189641422 85387 2 3 102520060 44897764 967513927 68691 0 6 303455737 35005212 521595369 89173 7 0 861021531 278722863 233665124 65783 7 1 801979803 315634023 635723059 13930 9 0 89018457 628175012 656478043 61394 9 0 914544920 60841378...
output:
269986411 687117872 337796106 649269006 273534477 925890819 789776059 781917067 471414212 683680813 655243026 766680733 110386800 920667633 42177293 33248798 268698025 97602241 455950431 787378605 628127823 884695308 910301084 481441390 301149571 40678494 744524425 997602040 853435603 942399367 4371...
result:
ok 1000 numbers
Test #9:
score: 10
Accepted
time: 31ms
memory: 3760kb
input:
1000 846930887 681692778 714636916 89384 3 3 649760493 596516650 189641422 85387 2 3 102520060 44897764 967513927 68691 0 6 303455737 35005212 521595369 89173 7 0 861021531 278722863 233665124 65783 7 1 801979803 315634023 635723059 13930 9 0 89018457 628175012 656478043 61394 9 0 914544920 60841378...
output:
269986411 687117872 337796106 649269006 273534477 925890819 789776059 781917067 471414212 683680813 655243026 766680733 110386800 920667633 42177293 33248798 268698025 97602241 455950431 787378605 628127823 884695308 910301084 481441390 301149571 40678494 744524425 997602040 853435603 942399367 4371...
result:
ok 1000 numbers
Test #10:
score: 10
Accepted
time: 30ms
memory: 3752kb
input:
1000 846930887 681692778 714636916 89384 3 3 649760493 596516650 189641422 85387 2 3 102520060 44897764 967513927 68691 0 6 303455737 35005212 521595369 89173 7 0 861021531 278722863 233665124 65783 7 1 801979803 315634023 635723059 13930 9 0 89018457 628175012 656478043 61394 9 0 914544920 60841378...
output:
269986411 687117872 337796106 649269006 273534477 925890819 789776059 781917067 471414212 683680813 655243026 766680733 110386800 920667633 42177293 33248798 268698025 97602241 455950431 787378605 628127823 884695308 910301084 481441390 301149571 40678494 744524425 997602040 853435603 942399367 4371...
result:
ok 1000 numbers