QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#128624 | #273. 类欧几里得算法 | myee# | 100 ✓ | 49ms | 3044kb | C++11 | 7.2kb | 2023-07-21 13:52:43 | 2023-07-21 13:52:43 |
Judging History
answer
// 那就是希望。
// 即便需要取模,也是光明。
#include <algorithm>
#include <random>
#include <stdio.h>
#include <vector>
typedef long long llt;
typedef unsigned uint;typedef unsigned long long ullt;
typedef bool bol;typedef char chr;typedef void voi;
typedef double dbl;
template<typename T>bol _max(T&a,T b){return(a<b)?a=b,true:false;}
template<typename T>bol _min(T&a,T b){return(b<a)?a=b,true:false;}
template<typename T>T lowbit(T n){return n&-n;}
template<typename T>T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<typename T>T lcm(T a,T b){return(a!=0||b!=0)?a/gcd(a,b)*b:(T)0;}
template<typename T>T exgcd(T a,T b,T&x,T&y){if(b!=0){T ans=exgcd(b,a%b,y,x);y-=a/b*x;return ans;}else return y=0,x=1,a;}
template<typename T>T power(T base,T index,T mod)
{
T ans=1%mod;
while(index)
{
if(index&1)ans=ans*base%mod;
base=base*base%mod,index>>=1;
}
return ans;
}
namespace ConstMod
{
template<const ullt p>
class mod_ullt
{
private:
ullt v;
inline ullt chg(ullt w){return(w<p)?w:w-p;}
inline mod_ullt _chg(ullt w){mod_ullt ans;ans.v=(w<p)?w:w-p;return ans;}
public:
mod_ullt():v(0){}
mod_ullt(ullt v):v(v%p){}
bol empty(){return!v;}
inline ullt val(){return v;}
friend bol operator<(mod_ullt a,mod_ullt b){return a.v<b.v;}
friend bol operator>(mod_ullt a,mod_ullt b){return a.v>b.v;}
friend bol operator<=(mod_ullt a,mod_ullt b){return a.v<=b.v;}
friend bol operator>=(mod_ullt a,mod_ullt b){return a.v>=b.v;}
friend bol operator==(mod_ullt a,mod_ullt b){return a.v==b.v;}
friend bol operator!=(mod_ullt a,mod_ullt b){return a.v!=b.v;}
inline friend mod_ullt operator+(mod_ullt a,mod_ullt b){return a._chg(a.v+b.v);}
inline friend mod_ullt operator-(mod_ullt a,mod_ullt b){return a._chg(a.v+a.chg(p-b.v));}
inline friend mod_ullt operator*(mod_ullt a,mod_ullt b){return a.v*b.v;}
friend mod_ullt operator/(mod_ullt a,mod_ullt b){return b._power(p-2)*a.v;}
friend mod_ullt operator^(mod_ullt a,ullt b){return a._power(b);}
inline mod_ullt operator-(){return _chg(p-v);}
mod_ullt sqrt()
{
if(power(v,(p-1)>>1,p)!=1)return 0;
mod_ullt b=1;do b++;while(b._power((p-1)>>1)==1);
ullt t=p-1,s=0,k=1;while(!(t&1))s++,t>>=1;
mod_ullt x=_power((t+1)>>1),e=_power(t);
while(k<s)
{
if(e._power(1llu<<(s-k-1))!=1)x*=b._power((1llu<<(k-1))*t);
e=_power(p-2)*x*x,k++;
}
return _min(x,-x),x;
}
mod_ullt inv(){return _power(p-2);}
mod_ullt _power(ullt index){mod_ullt ans(1),w(v);while(index){if(index&1)ans*=w;w*=w,index>>=1;}return ans;}
voi read(){v=0;chr c;do c=getchar();while(c>'9'||c<'0');do v=(c-'0'+v*10)%p,c=getchar();while(c>='0'&&c<='9');v%=p;}
voi print()
{
static chr C[20];uint tp=0;
ullt w=v;do C[tp++]=w%10+'0',w/=10;while(w);
while(tp--)putchar(C[tp]);
}
voi println(){print(),putchar('\n');}
mod_ullt operator++(int){mod_ullt ans=*this;return v=chg(v+1),ans;}
public:
inline ullt&operator()(){return v;}
inline mod_ullt&operator+=(mod_ullt b){return*this=_chg(v+b.v);}
inline mod_ullt&operator-=(mod_ullt b){return*this=_chg(v+chg(p-b.v));}
inline mod_ullt&operator*=(mod_ullt b){return*this=v*b.v;}
mod_ullt&operator^=(ullt b){return*this=_power(b);}
mod_ullt&operator/=(mod_ullt b){return*this=b._power(p-2)*v;}
mod_ullt&operator++(){return v=chg(v+1),*this;}
};
}
// Heaven and Earth... My guiding star...
// Akira Complex R.I.P.
const ullt Mod=1000000007;
typedef ConstMod::mod_ullt<Mod>modint;
typedef std::vector<modint>modvec;
inline modint turn(llt w){return w%(llt)Mod+Mod;}
modint P[15],Q[15];
inline modint binom(modint a,uint b)
{
modint ans=Q[b];for(uint j=0;j<b;j++)ans*=a-j;
return ans;
}
uint K;modint Ans[15][15],Base[15][15];
voi upd(llt n,llt a,llt b,llt c)
{
if(!n){
for(uint i=0;i<=K;i++)for(uint j=0;i+j<=K;j++)Ans[i][j]=0;
return;
}
if(!a){
modint g=turn((b-(b%c+c)%c)/c);
static modint User1[15],User2[15];
for(uint i=0;i<=K;i++)User1[i]=binom(n+i,i+1),User2[i]=binom(g+i-1,i);
for(uint i=0;i<=K;i++)for(uint j=0;i+j<=K;j++)Ans[i][j]=User1[i]*User2[j];
return;
}
if(a<0||b<0||a>=c||b>=c)
{
upd(n,(a%c+c)%c,(b%c+c)%c,c);
modint A=turn((a-(a%c+c)%c)/c),B=turn((b-(b%c+c)%c)/c);
static modint User[15][15],R[15][15];
for(uint i=0;i<=K;i++)for(uint j=0;i+j<=K;j++)R[i][j]=0;
for(uint i=0;i<=K;i++)for(uint j=0;j<=i;j++)User[i][j]=0;
User[0][0]=1;for(uint i=0;i<K;i++)for(uint j=0;j<=i;j++)User[i+1][j]+=User[i][j]*(i+B),User[i+1][j+1]+=User[i][j]*A;
for(uint i=0;i<=K;i++)for(uint j=0;j<=i;j++)User[i][j]*=Q[i];
for(uint i=0;i<=K;i++)for(uint j=0;i+j<=K;j++)
{
static modint T[15];for(uint q=0;q<=i+j;q++)T[q]=0;
for(uint p=0;p<=i;p++)for(uint q=0;q<=j;q++)T[p+q]+=Base[i][p]*User[j][q];
for(uint q=i+j;~q;q--)
{
modint w=T[q]*P[q];
for(uint s=0;i+j+s<=K;s++)R[i][j+s]+=w*Ans[q][s];
for(uint s=0;s<=q;s++)T[s]-=w*Base[q][s];
}
}
for(uint i=0;i<=K;i++)for(uint j=0;i+j<=K;j++)Ans[i][j]=R[i][j];
return;
}
upd((a*n+b)/c,c,-b-1,a);
modint g=turn((a*n+b)/c);
static modint User1[15],User2[15];
for(uint i=0;i<=K;i++)User1[i]=binom(n+i,i+1),User2[i]=binom(g+i-1,i);
static modint R[15][15];
for(uint i=0;i<=K;i++)for(uint j=0;i+j<=K;j++)R[i][j]=User1[i]*User2[j]-(j?Ans[j-1][i+1]:0);
for(uint i=0;i<=K;i++)for(uint j=0;i+j<=K;j++)Ans[i][j]=R[i][j];
}
modint S[15][15];
modint solve(llt n,llt a,llt b,llt c,uint k1,uint k2)
{
K=k1+k2,upd(n,a,b,c);
modint ans;
for(uint i=0;i<=k1;i++)for(uint j=0;j<=k2;j++)
ans+=((i^j^k1^k2)&1?-P[i]*P[j]*S[k1][i]*S[k2][j]:P[i]*P[j]*S[k1][i]*S[k2][j])*Ans[i][j];
if(!k1)ans+=modint(b/c)^k2;
return ans;
}
int main()
{
#ifdef MYEE
freopen("QAQ.in","r",stdin);
freopen("QAQ.out","w",stdout);
#endif
P[0]=1;for(uint i=1;i<=11;i++)P[i]=P[i-1]*i;
Q[11]=P[11].inv();for(uint i=11;i;i--)Q[i-1]=Q[i]*i;
Base[0][0]=1;for(uint i=0;i<10;i++)for(uint j=0;j<=i;j++)Base[i+1][j]+=Base[i][j]*i,Base[i+1][j+1]+=Base[i][j];
for(uint i=0;i<=10;i++)for(uint j=0;j<=i;j++)Base[i][j]*=Q[i];
S[0][0]=1;for(uint i=1;i<=10;i++)for(uint j=1;j<=i;j++)S[i][j]=S[i-1][j]*j+S[i-1][j-1];
uint t;scanf("%u",&t);
while(t--)
{
uint n,a,b,c,k1,k2;
scanf("%u%u%u%u%u%u",&n,&a,&b,&c,&k1,&k2);
solve(n,a,b,c,k1,k2).println();
}
return 0;
}
// 那就是希望。
// 即便需要取模,也是光明。
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 10
Accepted
time: 3ms
memory: 3040kb
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: 3ms
memory: 2996kb
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: 3ms
memory: 2880kb
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: 3ms
memory: 2992kb
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: 49ms
memory: 2880kb
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: 49ms
memory: 2940kb
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: 49ms
memory: 3044kb
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: 49ms
memory: 2948kb
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: 49ms
memory: 3008kb
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: 49ms
memory: 2968kb
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