原文链接www.cnblogs.com/zhouzhendong/p/UOJ75.html

前言

根本没想到。

题解

首先我们可以考虑一种做法:

找一些图,使得他们各自的生成树个数乘起来等于 k。

那么只要将他们用一条链连起来就得到答案了。

接下来考虑如何得到这些图。

考虑随机生成一个 n 个点的图,它的生成树个数最大是 $n^{n-2}$ 。

我们假装一个 n 个点的图的生成树个数是 $[0,n^{n-2}]$ 中的随机数。

假设我们随机生成了 S 个这样的图,如果我们在这 S 个图中随机选择 t 个连起来,那么我们就得到了 $S^t$ 个随机数。

令 n = 12, S = 1000, t = 4,由于 $n^{n-2} = 12^{10}> 62\cdot 998244353$,所以我们可以在对 998244353 取模之后把生成树个数当作一个 $[0,998244353)$ 中的随机数。则我们得到了 $S^t = 10^{12}$ 个随机数。这 $10^{12}$ 个数中不含有 k 个概率非常小。

我们不可能把所有所有的这些随机数都求出来,所以我们用类似于 BSGS 的办法折半一下就好了。

代码

#pragma GCC optimize("Ofast","inline")
#include <bits/stdc++.h>
#define clr(x) memset(x,0,sizeof (x))
#define For(i,a,b) for (int i=a;i<=b;i++)
#define Fod(i,b,a) for (int i=b;i>=a;i--)
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define fi first
#define se second
#define _SEED_ ('C'+'L'+'Y'+'A'+'K'+'I'+'O'+'I')
#define outval(x) printf(#x" = %d\n",x)
#define outvec(x) printf("vec "#x" = ");for (auto _v : x)printf("%d ",_v);puts("")
#define outtag(x) puts("----------"#x"----------")
#define outarr(a,L,R) printf(#a"[%d...%d] = ",L,R);\
For(_v2,L,R)printf("%d ",a[_v2]);puts("");
using namespace std;
typedef long long LL;
typedef vector <int> vi;
LL read(){
LL x=0,f=0;
char ch=getchar();
while (!isdigit(ch))
f|=ch=='-',ch=getchar();
while (isdigit(ch))
x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return f?-x:x;
}
const int N=1005,M=20,mod=998244353;
void Add(int &x,int y){
if ((x+=y)>=mod)
x-=mod;
}
void Del(int &x,int y){
if ((x-=y)<0)
x+=mod;
}
int Pow(int x,int y){
int ans=1;
for (;y;y>>=1,x=(LL)x*x%mod)
if (y&1)
ans=(LL)ans*x%mod;
return ans;
}
vector <pair <int,int> > g[N];
int val[N],ival[N];
int calc(int n,vector <pair <int,int> > &e){
static int a[M][M];
clr(a);
for (auto E : e){
int x=E.fi,y=E.se;
a[x][y]++,a[y][x]++;
a[x][x]--,a[y][y]--;
}
For(i,1,n)
For(j,1,n)
if (a[i][j]<0)
a[i][j]+=mod;
n--;
For(i,1,n){
For(j,i,n)
if (a[j][i]!=0){
For(k,i,n)
swap(a[i][k],a[j][k]);
break;
}
if (a[i][i]==0)
return 0;
For(j,i+1,n){
int v=(LL)a[j][i]*Pow(a[i][i],mod-2)%mod;
For(k,i,n)
Del(a[j][k],(LL)a[i][k]*v%mod);
}
}
int ans=mod-1;
For(i,1,n)
ans=(LL)ans*a[i][i]%mod;
return ans;
}
struct hash_map{
static const int Ti=233,mod=1<<20;
int cnt,k[mod+1],v[mod+1],nxt[mod+1],fst[mod+1];
int Hash(int x){
int v=x&(mod-1);
return v==0?mod:v;
}
void clear(){
cnt=0;
memset(fst,0,sizeof fst);
}
void update(int x,int a){
int y=Hash(x);
for (int p=fst[y];p;p=nxt[p])
if (k[p]==x){
v[p]=a;
return;
}
k[++cnt]=x,nxt[cnt]=fst[y],fst[y]=cnt,v[cnt]=a;
return;
}
int find(int x){
int y=Hash(x);
for (int p=fst[y];p;p=nxt[p])
if (k[p]==x)
return v[p];
return 0;
}
int &operator [] (int x){
int y=Hash(x);
for (int p=fst[y];p;p=nxt[p])
if (k[p]==x)
return v[p];
k[++cnt]=x,nxt[cnt]=fst[y],fst[y]=cnt;
return v[cnt]=0;
}
}Map;
int S=1000;
void prework(){
srand(_SEED_);
int n=12;
For(i,1,S){
For(j,1,n)
For(k,1,j-1)
if (rand()%10>=2)
g[i].pb(mp(j,k));
val[i]=calc(n,g[i]);
ival[i]=Pow(val[i],mod-2);
}
Map.clear();
For(i,1,S)
For(j,1,i){
int tmp=(LL)val[i]*val[j]%mod;
Map[tmp]=i*(S+1)+j;
}
}
void solve(int x){
if (!x)
return (void)puts("2 0");
For(i,1,S)
For(j,1,i){
int tmp=(LL)x*ival[i]%mod*ival[j]%mod;
if (Map[tmp]){
int v[4]={i,j,Map[tmp]/(S+1),Map[tmp]%(S+1)};
vector <pair <int,int> > res;
res.clear();
int n=48,cnt=0;
For (k,0,3){
if (cnt>0)
res.pb(mp(cnt,cnt+1));
for (auto e : g[v[k]])
res.pb(mp(e.fi+cnt,e.se+cnt));
cnt+=12;
}
printf("%d %d\n",n,(int)res.size());
for (auto e : res)
printf("%d %d\n",e.fi,e.se);
return;
}
}
}
int main(){
prework();
int T=read();
while (T--)
solve(read());
return 0;
}

  

UOJ#75. 【UR #6】智商锁 随机化算法 矩阵树定理的更多相关文章

  1. 【UOJ#75】【UR #6】智商锁(矩阵树定理,随机)

    [UOJ#75][UR #6]智商锁(矩阵树定理,随机) 题面 UOJ 题解 这种题我哪里做得来啊[惊恐],,, 题解做法:随机\(1000\)个点数为\(12\)的无向图,矩阵树定理算出它的生成树个 ...

  2. UOJ 75 - 【UR #6】智商锁(矩阵树定理+随机+meet-in-the-middle)

    题面传送门 一道很神的矩阵树定理+乱搞的题 %%%%%%%%%%%%%%% vfk yyds u1s1 这种题目我是根本想不出来/kk,大概也就 jgh 这样的随机化带师才能想到出来吧 首先看到生成树 ...

  3. 【构造 meet in middle 随机 矩阵树定理】#75. 【UR #6】智商锁

    没智商了 变式可见:[构造 思维题]7.12道路建设 当你自信满满地把你认为的正确密码输入后,时光机滴滴报警 —— 密码错误.你摊坐在了地上. 黑衣人满意地拍了拍你的肩膀:“小伙子,不错嘛.虽然没解开 ...

  4. UOJ#121. 【NOI2013】向量内积 随机化算法,矩阵

    原文链接www.cnblogs.com/zhouzhendong/UOJ121.html 前言 完蛋了我越来越菜了贺题都不会了. 题解 $O(n ^ 2 d) $ 暴力送 60 分. Bitset 优 ...

  5. 【算法】Matrix - Tree 矩阵树定理 & 题目总结

    最近集中学习了一下矩阵树定理,自己其实还是没有太明白原理(证明)类的东西,但想在这里总结一下应用中的一些细节,矩阵树定理的一些引申等等. 首先,矩阵树定理用于求解一个图上的生成树个数.实现方式是:\( ...

  6. 算法复习——矩阵树定理(spoj104)

    题目: In some countries building highways takes a lot of time... Maybe that's because there are many p ...

  7. POJ 矩阵相乘 (随机化算法-舍伍德(Sherwood))

    周三的算法课,主要讲了随机化算法,介绍了拉斯维加斯算法,简单的理解了为什么要用随机化算法,随机化算法有什么好处. 在处理8皇后问题的时候,穷举法是最费时的,回朔比穷举好点,而当数据量比较大的时候,如1 ...

  8. POJ3318--Matrix Multiplication 随机化算法

    Description You are given three n × n matrices A, B and C. Does the equation A × B = C hold true? In ...

  9. 2018.09.14 codeforces364D(随机化算法)

    传送门 根据国家集训队2014论文集中胡泽聪的随机化算法可以通过这道题. 对于每个数,它有12" role="presentation" style="posi ...

随机推荐

  1. Linux sed command

    概述 sed 是一种在线非交互式编辑器,它一次处理一行内容. 处理时,把当前处理的行存储在临时缓冲区中,称为"模式空间"(pattern space). 接着用sed命令处理缓冲区 ...

  2. Linux基础整理

    命令 说明 chsh 查看和修改当前登录的Shell export 查看和设置Shell环境变量 read 读取从键盘或文件输入的数据 expr 四则远算和字符串运算 tmux 一个窗口操作多个会话 ...

  3. KFold,StratifiedKFold k折交叉切分

    python风控评分卡建模和风控常识(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005214003&am ...

  4. 011 Socket定义客户端

    引入命名空间: using System.Net; using System.Net.Sockets; using System.Threading;

  5. 使用numpy的小惊喜

    今天使用 numpy.true_divide 发现个有趣的事情, 下面的代码18.19行如果去掉,就会报下面的  RuntimeWarning def multivalue_divide(timese ...

  6. css/css3 未知元素宽高,垂直居中和水平居中

    未知元素的宽高情况下  垂直居中和水平居中 第一种 flex盒布局 (推荐) /*弹性盒模型*/ /*主轴居中对齐*/ /*侧轴居中对齐*/ .ele{ display:flex; justify-c ...

  7. mysql 一张表的数据插入另一张表的sql语句

    1. 表结构完全一样 insert into 表1 select * from 表2 2. 表结构不一样(这种情况下得指定列名) insert into 表(列名1,列名2,列名3) select 列 ...

  8. 【OpenGL】代码记录01创建窗口

    创建空窗口: #include<iostream> // GLEW #define GLEW_STATIC #include <GL/glew.h> // GLFW #incl ...

  9. git出现refusing to merge unrelated histories

    问题描述当本地分支与远程分支没有共同祖先时,会出现 fatal: refusing to merge unrelated histories 的问题. 解决方案可以使用 rebase 的方式来进行合并 ...

  10. Apache Storm

    作者:jiangzz 电话:15652034180 微信:jiangzz_wx 微信公众账号:jiangzz_wy 背景介绍 流计算:将大规模流动数据在不断变化的运动过程中实现数据的实时分析,捕捉到可 ...