QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#232280 | #7613. Inverse Problem | ExplodingKonjac | TL | 1ms | 3620kb | C++20 | 9.9kb | 2023-10-30 09:18:26 | 2023-10-30 09:18:28 |
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=6;
int T;
Mint p0[15],p[15],f[205],g[205];
bitset<MOD> vis;
bool found[15];
vector<int> ans[15];
map<pair<int,Mint>,int> dfs_vis;
int cur_tim;
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);
if(dfs_vis[val]==cur_tim) return;
dfs_vis[val]=cur_tim;
res.push_back(val);
if(dep>cnt) return;
for(int i=L;i<=R;i++)
{
if(sum+i>lim) break;
dfs1(cnt,lim,L,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,const multimap<pair<int,Mint>,int> &qry,int dep=1,int sum=0,Mint prd=1)
{
static int a[205];
pair val(sum,prd);
if(dfs_vis[val]==cur_tim) return;
dfs_vis[val]=cur_tim;
for(auto it=qry.lower_bound(val);it!=qry.end() && it->first==val;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,L,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};
}
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});
cur_tim++,dfs2(n,n-2,1,n-2,false,mp);
}
else
{
vector<pair<int,Mint>> vec1,vec2;
multimap<pair<int,Mint>,int> mp1,mp2;
cur_tim++,dfs1(n,n-2,1,B,false,vec1);
cur_tim++,dfs1(n,n-2,B+1,n-2,true,vec2);
for(int s1=0;s1<=n-2;s1++)
{
for(auto &[sum,prd]: vec1) if(sum==s1) vis.set(prd());
for(auto &[sum,prd]: vec2)
{
if(sum!=n-2-s1) continue;
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) if(sum==s1) vis.reset(prd());
}
cur_tim++,dfs2(n,n-2,1,B,false,mp1);
cur_tim++,dfs2(n,n-2,B+1,n-2,true,mp2);
}
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;
}
/*
4
2
360
1
509949433
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3620kb
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: -100
Time Limit Exceeded
input:
9 185396120 468170792 837583517 696626231 338497514 762842660 800028852 928391161 733524004