QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#232287 | #7613. Inverse Problem | ExplodingKonjac | AC ✓ | 31439ms | 325192kb | C++20 | 9.7kb | 2023-10-30 09:30:19 | 2023-10-30 09:30:21 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
// #define OPENIOBUF
namespace FastIO
{
class FastIOBase
{
protected:
#ifdef OPENIOBUF
static const int BUFSIZE=1<<16;
char buf[BUFSIZE+1];
int buf_p=0;
#endif
FILE *target;
FastIOBase(FILE *f): target(f){}
~FastIOBase()=default;
public:
#ifdef OPENIOBUF
virtual void flush()=0;
#endif
};
class FastOutput final: public FastIOBase
{
public:
FastOutput(FILE *f=stdout): FastIOBase(f) {}
#ifdef OPENIOBUF
~FastOutput() { flush(); }
void flush() { fwrite(buf,1,buf_p,target),buf_p=0; }
#endif
void put(char x)
{
#ifdef OPENIOBUF
if(buf[buf_p++]=x,buf_p==BUFSIZE) flush();
#else
putc(x,target);
#endif
}
FastOutput &operator <<(char x)
{ return put(x),*this; }
FastOutput &operator <<(const char *s)
{ for(;*s;put(*(s++)));return *this; }
FastOutput &operator <<(const std::string &s)
{ return (*this)<<s.c_str(); }
template<typename T>
std::enable_if_t<std::is_integral<T>::value,FastOutput&> operator <<(T x)
{
if(x<0) return put('-'),(*this)<<(-x);
char stk[numeric_limits<T>::digits10+1],*top=stk;
do *(top++)=x%10+'0',x/=10; while(x);
while(top!=stk) put(*(--top));
return *this;
}
template<typename ...T>
void writesp(T &&...x)
{ std::initializer_list<int>{((*this)<<(x),put(' '),0)...}; }
template<typename ...T>
void writeln(T &&...x)
{ std::initializer_list<int>{((*this)<<(x),put('\n'),0)...}; }
template<typename Iter>
void writesp(Iter begin,Iter end)
{ while(begin!=end) (*this)<<*(begin++)<<' '; }
template<typename Iter>
void writeln(Iter begin,Iter end)
{ while(begin!=end) (*this)<<*(begin++)<<'\n'; }
}qout;
class FastInput final: public FastIOBase
{
private:
bool __eof;
public:
FastInput(FILE *f=stdin): FastIOBase(f),__eof(false)
#ifdef OPENIOBUF
{ buf_p=BUFSIZE; }
void flush() { buf[fread(buf,1,BUFSIZE,target)]=EOF,buf_p=0; }
bool eof()const { return buf[buf_p]==EOF; }
#else
{}
bool eof()const { return feof(target); }
#endif
char get()
{
if(__eof) return EOF;
#ifdef OPENIOBUF
if(buf_p==BUFSIZE) flush();
char ch=buf[buf_p++];
#else
char ch=getc(target);
#endif
return ~ch?ch:(__eof=true,EOF);
}
void unget(char c)
{
__eof=false;
#ifdef OPENIOBUF
buf[--buf_p]=c;
#else
ungetc(c,target);
#endif
}
explicit operator bool()const { return !__eof; }
FastInput &operator >>(char &x)
{ while(isspace(x=get()));return *this; }
template<typename T>
std::enable_if_t<std::is_integral<T>::value,FastInput&> operator >>(T &x)
{
char ch,sym=0;x=0;
while(isspace(ch=get()));
if(__eof) return *this;
if(ch=='-') sym=1,ch=get();
for(;isdigit(ch);x=(x<<1)+(x<<3)+(ch^48),ch=get());
return unget(ch),sym?x=-x:x,*this;
}
FastInput &operator >>(char *s)
{
while(isspace(*s=get()));
if(__eof) return *this;
for(;!isspace(*s) && !__eof;*(++s)=get());
return unget(*s),*s='\0',*this;
}
FastInput &operator >>(std::string &s)
{
char str_buf[(1<<8)+1]={0},*p=str_buf;
char *const buf_end=str_buf+(1<<8);
while(isspace(*p=get()));
if(__eof) return *this;
for(s.clear(),p++;;p=str_buf)
{
for(;p!=buf_end && !isspace(*p=get()) && !__eof;p++);
if(p!=buf_end) break;
s.append(str_buf);
}
unget(*p),*p='\0',s.append(str_buf);
return *this;
}
template<typename ...T>
void read(T &&...x)
{ std::initializer_list<int>{((*this)>>(x),0)...}; }
template<typename Iter>
void read(Iter begin,Iter end)
{ while(begin!=end) (*this)>>*(begin++); }
}qin;
} // namespace FastIO
using FastIO::qin,FastIO::qout;
using LL=long long;
using LD=long double;
using UI=unsigned int;
using ULL=unsigned long long;
#ifndef DADALZY
#define FILEIO(file) freopen(file".in","r",stdin),freopen(file".out","w",stdout)
#define LOG(...) [](auto...){}(__VA_ARGS__)
#else
#define FILEIO(file)
#define LOG(...) fprintf(stderr,__VA_ARGS__)
#endif
namespace Math
{
template<typename Derived>
class ModintBase
{
#define DEF_OP1(op,expr) \
friend constexpr Derived operator op(const Derived &lhs,const Derived &rhs) \
{ return Derived(lhs)op##=rhs; } \
constexpr Derived &operator op##=(const Derived &rhs) \
{ return (expr),*static_cast<Derived*>(this); }
#define DEF_OP2(op,expr) \
constexpr Derived operator op(int) \
{ Derived res(*static_cast<Derived*>(this)); return op(*this),res; } \
constexpr Derived &operator op() \
{ return (expr),*static_cast<Derived*>(this); }
#define DEF_OP3(op,expr) \
constexpr Derived operator op()const \
{ return (expr); }
#define DEF_OP4(op) \
friend constexpr bool operator op(const Derived &lhs,const Derived &rhs) \
{ return lhs.val op rhs.val; }
#define MOD Derived::getMod()
protected:
int val;
private:
template<typename T>
static constexpr std::enable_if_t<std::is_integral<T>::value,T> __inv(T a,T b)
{ T x=0,y=1,t=0;for(;a;t=b/a,std::swap(a,b-=t*a),std::swap(y,x-=t*y));return x; }
template<typename T>
static constexpr std::enable_if_t<std::is_integral<T>::value,T> __fix(T x)
{ return Derived::fix(x); }
static constexpr void __qmo(int &x)
{ x+=(x>>31&MOD); }
public:
constexpr ModintBase(): val(0) {}
constexpr ModintBase(const Derived &x): val(x.val) {}
template<typename T>
constexpr ModintBase(T x): val(__fix(x)) {}
constexpr static Derived unfixed(int x)
{ return reinterpret_cast<Derived&>(x); }
constexpr Derived inv()const
{ return Derived(__inv(val,MOD)); }
constexpr int operator ()()const
{ return val; }
DEF_OP1(+,__qmo(val+=rhs.val-MOD))
DEF_OP1(-,__qmo(val-=rhs.val))
DEF_OP1(*,(val=__fix(1ll*val*rhs.val)))
DEF_OP1(/,(val=__fix(1ll*val*__inv(rhs.val,MOD))))
DEF_OP2(++,__qmo(val+=1-MOD))
DEF_OP2(--,__qmo(--val))
DEF_OP3(+,*this)
DEF_OP3(-,unfixed(val?MOD-val:0))
DEF_OP4(==) DEF_OP4(!=) DEF_OP4(<) DEF_OP4(>) DEF_OP4(<=) DEF_OP4(>=)
#undef DEF_OP1
#undef DEF_OP2
#undef DEF_OP3
#undef DEF_OP4
#undef MOD
};
template<typename T,typename U>
constexpr std::enable_if_t<std::is_integral<T>::value,U> qpow(U x,T y)
{ U res(1);for(;y;x*=x,y>>=1)if(y&1)res*=x;return res; }
class FastMod
{
private:
using ULL=uint64_t;
using U128=__uint128_t;
ULL p,m;
public:
constexpr FastMod(): p(0),m(0) {}
constexpr FastMod(ULL p): p(p),m((U128(1)<<64)/p) {}
constexpr ULL getp()const { return p; }
constexpr ULL operator()(ULL a)const
{ ULL q=(U128(m)*a)>>64,r=a-q*p;return r>=p?r-p:r; }
};
// Modint for dynamic MOD
class DModint: public ModintBase<DModint>
{
private:
using BaseT=ModintBase<DModint>;
static FastMod R;
friend BaseT;
template<typename T> static constexpr T fix(T x)
{ return x<0?R.getp()-R(-x):R(x); }
public:
using BaseT::BaseT;
static constexpr void setMod(int mod) { R=FastMod(mod); }
static constexpr int getMod() { return R.getp(); }
};
FastMod DModint::R{};
// Modint for static MOD
template<int MOD>
class SModint: public ModintBase<SModint<MOD>>
{
private:
using BaseT=ModintBase<SModint<MOD>>;
friend BaseT;
template<typename T> static constexpr T fix(T x)
{ return (x%=MOD)<0?x+MOD:x; }
public:
using BaseT::BaseT;
static constexpr int getMod() { return MOD; }
};
} // namespace Math
constexpr int MOD=1e9+7;
using Mint=Math::SModint<MOD>;
constexpr int MAXN=125,B=5;
int T;
Mint p0[15],p[15],f[205],g[205];
bitset<MOD> vis;
bool found[15];
vector<int> ans[15];
void dfs1(int cnt,int lim,int L,int R,bool inv,vector<pair<int,Mint>> *res,int dep=1,int sum=0,Mint prd=1)
{
pair val(sum,prd);
res[sum].push_back(val);
if(dep>cnt) return;
for(int i=L;i<=R;i++)
{
if(sum+i>lim) break;
dfs1(cnt,lim,i,R,inv,res,dep+1,sum+i,prd*(inv?g[i]:f[i]));
}
}
void dfs2(int cnt,int lim,int L,int R,bool inv,multimap<pair<int,Mint>,int> &qry,int dep=1,int sum=0,Mint prd=1)
{
static int a[205];
pair val(sum,prd);
for(auto it=qry.lower_bound(val);it!=qry.end() && it->first==val;qry.erase(it++))
{
for(int i=1;i<dep;i++)
ans[it->second].push_back(a[i]);
found[it->second]=true;
}
if(dep>cnt) return;
for(int i=L;i<=R;i++)
{
if(sum+i>lim) break;
a[dep]=i;
dfs2(cnt,lim,i,R,inv,qry,dep+1,sum+i,prd*(inv?g[i]:f[i]));
}
}
int main()
{
qin>>T;
for(int i=1;i<=T;i++)
{
qin>>reinterpret_cast<int&>(p0[i]);
if(p0[i]()==1) found[i]=1,ans[i]={-1};
}
vector<pair<int,Mint>> vec1[205],vec2[205];
for(int n=2;n<=MAXN;n++)
{
for(int i=1;i<=T;i++)
p[i]=p0[i]/(Mint(n)*(n-1));
for(int i=0;i<n;i++)
{
f[i]=(i?f[i-1]*(n-1-i):1);
g[i]=f[i].inv();
}
if(n<=B)
{
multimap<pair<int,Mint>,int> mp;
for(int i=1;i<=T;i++)
if(!found[i])
mp.insert({{n-2,p[i]},i});
dfs2(n,n-2,1,n-2,false,mp);
}
else
{
multimap<pair<int,Mint>,int> mp1,mp2;
for(int i=0;i<=n-2;i++)
vec1[i].clear(),vec2[i].clear();
dfs1(n,n-2,1,B,false,vec1);
dfs1(n,n-2,B+1,n-2,true,vec2);
for(int s1=0;s1<=n-2;s1++)
{
for(auto &[sum,prd]: vec1[s1]) vis.set(prd());
for(auto &[sum,prd]: vec2[n-2-s1])
{
for(int i=1;i<=T;i++)
{
if(found[i]) continue;
Mint tmp=prd*p[i];
if(vis.test(tmp()))
{
mp1.insert({{s1,tmp},i});
mp2.insert({{sum,prd},i});
found[i]=true;
}
}
}
for(auto &[sum,prd]: vec1[s1]) vis.reset(prd());
}
if(!mp1.empty())
{
dfs2(n,n-2,1,B,false,mp1);
dfs2(n,n-2,B+1,n-2,true,mp2);
}
}
cerr<<"n = "<<n<<'\n';
if(count(found+1,found+T+1,false)==0)
break;
}
for(int o=1;o<=T;o++)
{
assert(found[o]);
int n=accumulate(ans[o].begin(),ans[o].end(),2);
ans[o].resize(n,0);
vector<pair<int,int>> t;
for(int i=0,j=1;i<ans[o].size();i++)
{
int cnt=ans[o][i]+(i==0);
while(cnt--) t.emplace_back(i+1,++j);
}
qout<<n<<'\n';
for(auto &[u,v]: t) qout<<u<<' '<<v<<'\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3632kb
input:
4 2 360 1 509949433
output:
2 1 2 5 1 2 1 3 2 4 2 5 1 10 1 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10
result:
ok OK (4 test cases)
Test #2:
score: 0
Accepted
time: 16263ms
memory: 324552kb
input:
9 185396120 468170792 837583517 696626231 338497514 762842660 800028852 928391161 733524004
output:
14 1 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 11 9 12 10 13 10 14 122 1 2 1 3 2 4 3 5 4 6 5 7 6 8 6 9 7 10 7 11 8 12 8 13 9 14 9 15 9 16 10 17 10 18 10 19 11 20 11 21 11 22 12 23 12 24 12 25 12 26 13 27 13 28 13 29 13 30 14 31 14 32 14 33 14 34 14 35 15 36 15 37 15 38 15 39 15 40 15 41 15 42 16 43 16 44...
result:
ok OK (9 test cases)
Test #3:
score: 0
Accepted
time: 31439ms
memory: 325192kb
input:
10 338497514 733524004 447182954 415993605 453460670 50499055 648088551 986982752 907925397 315315230
output:
124 1 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 11 10 12 11 13 11 14 12 15 12 16 12 17 13 18 13 19 13 20 13 21 13 22 14 23 14 24 14 25 14 26 14 27 14 28 15 29 15 30 15 31 15 32 15 33 15 34 15 35 16 36 16 37 16 38 16 39 16 40 16 41 16 42 17 43 17 44 17 45 17 46 17 47 17 48 17 49 18 50 18 51 18 52 18 53 18...
result:
ok OK (10 test cases)
Test #4:
score: 0
Accepted
time: 2403ms
memory: 160452kb
input:
10 1 2 3 4 5 6 7 8 9 10
output:
1 2 1 2 102 1 2 1 3 2 4 3 5 4 6 4 7 5 8 5 9 6 10 6 11 7 12 7 13 8 14 8 15 9 16 9 17 9 18 9 19 9 20 9 21 10 22 10 23 10 24 10 25 10 26 10 27 10 28 10 29 11 30 11 31 11 32 11 33 11 34 11 35 11 36 11 37 11 38 12 39 12 40 12 41 12 42 12 43 12 44 12 45 12 46 12 47 12 48 13 49 13 50 13 51 13 52 13 53 13 5...
result:
ok OK (10 test cases)
Test #5:
score: 0
Accepted
time: 654ms
memory: 137508kb
input:
10 269199917 392009324 753889928 751355133 472639410 132096559 331333826 40414701 72847302 475706026
output:
55 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 1 26 1 27 1 28 1 29 1 30 1 31 1 32 1 33 1 34 1 35 1 36 1 37 1 38 1 39 1 40 1 41 1 42 1 43 1 44 1 45 1 46 1 47 1 48 1 49 1 50 1 51 1 52 1 53 1 54 1 55 84 1 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 ...
result:
ok OK (10 test cases)
Extra Test:
score: 0
Extra Test Passed