置换的快速幂
题解
n2logn
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define cl(x) memset(x,0,sizeof(x))
using namespace std;
typedef long long ll;
inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}
inline void read(int &x){
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc());
}
const int N=205;
int n,p;
struct Te{
int a[N];
Te(){ }
Te(int t){
if (t==1) for (int i=1;i<=n;i++) a[i]=i;
}
int &operator [](int x){
return a[x];
}
friend bool operator == (Te A,Te B){
for (int i=1;i<=n;i++) if (A[i]!=B[i]) return 0;
return 1;
}
friend Te operator * (Te A,Te B){
Te ret;
for (int i=1;i<=n;i++) ret[i]=B[A[i]];
return ret;
}
friend Te Inv(Te A){
Te ret;
for (int i=1;i<=n;i++) ret[A[i]]=i;
return ret;
}
}T[N];
Te head,step,deah,tem;
namespace Sun{
inline ll gcd(ll a,ll b,ll& x,ll& y){
if (b==0){
x=1; y=0; return a;
}
ll temp,d=gcd(b,a%b,x,y);
temp=x;
x=y; y=temp-a/b*y;
return d;
}
inline ll solve(ll a,ll b,ll n){
ll sol,x,y;
ll d=gcd(a,n,x,y);
if (b%d==0){
sol=x*b/d;
ll r=n/d;
sol=(sol%r+r)%r;
return sol;
}else
return -1;
}
inline bool merge(ll a1,ll n1,ll a2,ll n2,ll &a,ll &n){
ll x,y,c=a2-a1;
ll d=gcd(n1,n2,x,y);
ll K=solve(n1,c,n2);
if (K==-1)
return false;
a=a1+n1*K;
n=n1*n2/d;
return true;
}
}
int da[N],dn[N]; ll Aa,Nn;
int tot;
inline ll Solve(){
int flag=0; using namespace Sun;
Aa=da[1],Nn=dn[1];
for (int i=2;i<=tot;i++)
if (!flag)
if (!merge(Aa,Nn,(ll)da[i],(ll)dn[i],Aa,Nn))
flag=1;
if (!flag)
return Aa%Nn;
else
return 1e9+1;
}
int lst[N],pnt;
int pos[N],vst[N];
int main(){
ll ans=1e9+1;
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
read(n); read(p);
for (int i=1;i<=n;i++) for (int j=1;j<=p;j++) read(T[j][i]);
head=Te(1);
for (int k=0;k<p;k++){
step=Te(1);
for (int i=k+1;i<=p;i++) step=step*T[i];
for (int i=1;i<=k;i++) step=step*T[i];
deah=Inv(head);
int flag=0;
for (int i=1;i<=n && !flag;i++)
if (!vst[i]){
pnt=0; lst[++pnt]=step[i];
while (step[lst[pnt]]!=i)
lst[pnt+1]=step[lst[pnt]],pnt++;
if (lst[pnt]!=i) lst[++pnt]=i;
for (int j=1;j<=pnt;j++) pos[lst[j]]=j,vst[lst[j]]=1;
if (!pos[deah[i]]) { flag=1; break; }
int k=(pos[deah[i]]-pos[i]+pnt)%pnt;
for (int j=2;j<=pnt;j++){
int u=lst[j];
if (!pos[u] || (pos[deah[u]]-pos[u]+pnt)%pnt!=k){
flag=1; break;
}
}
if (!flag)
da[++tot]=k,dn[tot]=pnt;
for (int j=1;j<=pnt;j++) pos[lst[j]]=0;
}
cl(vst);
if (!flag){
Solve();
if (!k && !Aa)
ans=min(ans,Nn*p);
else
ans=min(ans,Aa*p+k);
}
head=head*T[k+1];
tot=0;
}
if (ans==1e9+1)
printf("No one knows.\n");
else
printf("%lld\n",ans);
return 0;
}
n3大暴力
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef long long ll;
inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}
inline void read(int &x){
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc());
}
const int N=205;
int n,p;
struct Te{
int a[N];
Te(){ }
Te(int t){
if (t==1) for (int i=1;i<=n;i++) a[i]=i;
}
int &operator [](int x){
return a[x];
}
friend bool operator == (Te A,Te B){
for (int i=1;i<=n;i++) if (A[i]!=B[i]) return 0;
return 1;
}
friend Te operator * (Te A,Te B){
Te ret;
for (int i=1;i<=n;i++) ret[i]=B[A[i]];
return ret;
}
friend Te Inv(Te A){
Te ret;
for (int i=1;i<=n;i++) ret[A[i]]=i;
return ret;
}
}T[N];
Te head,step,deah,tem;
namespace Sun{
inline ll gcd(ll a,ll b,ll& x,ll& y){
if (b==0){
x=1; y=0; return a;
}
ll temp,d=gcd(b,a%b,x,y);
temp=x;
x=y; y=temp-a/b*y;
return d;
}
inline ll solve(ll a,ll b,ll n){
ll sol,x,y;
ll d=gcd(a,n,x,y);
if (b%d==0){
sol=x*b/d;
ll r=n/d;
sol=(sol%r+r)%r;
return sol;
}else
return -1;
}
inline bool merge(ll a1,ll n1,ll a2,ll n2,ll &a,ll &n){
ll x,y,c=a2-a1;
ll d=gcd(n1,n2,x,y);
ll K=solve(n1,c,n2);
if (K==-1)
return false;
a=a1+n1*K;
n=n1*n2/d;
return true;
}
}
int da[N],dn[N];
inline ll Solve(){
int flag=0; using namespace Sun;
ll A=da[1],N=dn[1];
for (int i=2;i<=n;i++)
if (!flag)
if (!merge(A,N,(ll)da[i],(ll)dn[i],A,N))
flag=1;
if (!flag)
return A%N==0?N:A%N;
else
return 1e9+1;
}
int main(){
ll ans=1e9+1;
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
read(n); read(p);
for (int i=1;i<=n;i++) for (int j=1;j<=p;j++) read(T[j][i]);
head=Te(1);
for (int k=0;k<p;k++){
step=Te(1);
for (int i=k+1;i<=p;i++) step=step*T[i];
for (int i=1;i<=k;i++) step=step*T[i];
deah=Inv(head);
for (int i=1;i<=n;i++){
int t=i,final=deah[i];
if (t==final) da[i]=0;
dn[i]=1; t=step[t];
while (t!=i){
if (t==final) da[i]=dn[i];
t=step[t];
dn[i]++;
}
}
ans=min(ans,Solve()*p+k);
head=head*T[k+1];
}
if (ans==1e9+1)
printf("No one knows.\n");
else
printf("%lld\n",ans);
return 0;
}