【BZOJ】2172: Mario填格子
题意
\(3 * 3\)的网格,给出左上角的数字\(m\)和右下角的数字\(m\),如果当前格子有数字\(x\),格子左边有个数字\(y\),格子上面有个数字\(z\),则\(y|x, z|x\)。格子中不存在相同的两个数。问是否存在填满格子的方案。
分析
最优放法肯定是每一个格子放的数相是上面和左边的数的最小公倍数或乘上了一个质因子。
首先我们质因子肯定是\(p=m/n\)的质因子(如果\(m \% n \neq 0\)则显然无解)而且可以把左上角看成\(1\),右下角看成\(p\)。
(下面假设\(a, b, c, d\)是\(p\)的质因子,且个数依次递增)
如果\(p\)的质因子种类超过\(3\)种,则显然成立。
$$
\begin{matrix}
1 & a & ad \\
b & ab & abd \\
bc & abc & p \\
\end{matrix}
$$
如果\(p\)的质因子种类只有\(3\)种,\(c\)的个数要超过\(1\)。
$$
\begin{matrix}
1 & c & cc \\
b & bc & bcc \\
ab & abc & p \\
\end{matrix}
$$
如果\(p\)的质因子种类只有\(2\)种,\(a, b\)的个数要超过\(1\)或者\(b\)的个数超过\(4\)种。
$$
\begin{matrix}
1 & a & aa \\
b & ab & aab \\
bb & abb & p \\
\end{matrix}
$$
$$
\begin{matrix}
1 & a & ab \\
b & abb & abbb \\
bb & abbbb & p \\
\end{matrix}
$$
如果\(p\)的质因子种类只有\(1\)种,\(a\)的个数要超过\(7\)。
$$
\begin{matrix}
1 & a & aa \\
aaa & aaaa & aaaaaaa \\
aaaaa & aaaaaa & p \\
\end{matrix}
$$
题解
分解质因数用\(Pollard-Rho\),然后判断即可。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll rand(ll l, ll r) {
static ll mo=1e9+7, g=78125, now=199811;
return l+((now*=g)%=mo)%(r-l+1);
}
ll mul(ll a, ll b, ll mo) {
a%=mo;
ll x=0;
for(; b; b>>=1) {
if(b&1) {
x+=a;
if(x>=mo) {
x-=mo;
}
}
a<<=1;
if(a>=mo) {
a-=mo;
}
}
return x;
}
ll ipow(ll a, ll b, ll mo) {
ll x=1;
for(; b; b>>=1, a=mul(a, a, mo)) {
if(b&1) {
x=mul(x, a, mo);
}
}
return x;
}
bool check(ll n) {
if(n==2 || n==3 || n==5 || n==7 || n==11 || n==13 || n==17 || n==19) {
return 1;
}
if(n%2==0 || n%3==0 || n%5==0 || n%7==0 || n%11==0 || n%13==0 || n%17==0 || n%19==0) {
return 0;
}
ll d=n-1;
int cnt=0;
for(; !(d&1); d>>=1, ++cnt);
for(int tt=0; tt<15; ++tt) {
ll now=ipow(rand(2, n-1), d, n), pre=now;
for(int i=0; i<cnt; ++i) {
now=mul(now, now, n);
if(now==1 && pre!=1 && pre!=n-1) {
return 0;
}
pre=now;
}
if(now!=1) {
return 0;
}
}
return 1;
}
ll gcd(ll a, ll b) {
return b?gcd(b, a%b):a;
}
ll poi(ll n, ll c) {
for(int i=2; i<=20; ++i) {
if(n%i==0) {
return i;
}
}
ll x=rand(1, n-1), y=x, g;
for(int k=2, i=2; ; ++i) {
x=mul(x, x, n)+c;
if(x>=n) {
x-=n;
}
if((g=gcd(n, (y-x+n)%n))!=1) {
return g;
}
if(x==y) {
return n;
}
if(k==i) {
y=x;
k<<=1;
}
}
}
void po(ll n, ll f[], bool flag=1) {
if(n==1) {
return;
}
if(check(n)) {
if(flag) {
return;
}
f[++f[0]]=n;
return;
}
ll d;
while((d=poi(n, rand(1, n-1)))==n);
po(d, f, 0);
po(n/d, f, 0);
}
ll f[35], a[35], n, m;
int num;
int main() {
while(~scanf("%lld%lld", &n, &m)) {
if(m%n) {
puts("Wario_wins!\n");
continue;
}
m/=n;
memset(f, 0, sizeof f);
num=0;
po(m, f);
if(f[0]) {
sort(f+1, f+1+f[0]);
a[num=1]=1;
for(int i=2; i<=f[0]; ++i) {
if(f[i]!=f[i-1]) {
a[++num]=1;
}
else {
a[num]++;
}
}
sort(a+1, a+1+num);
}
bool flag=num>=4 || (num==3&&a[num]>1) || (num==2&&((a[num]>1&&a[num-1]>1)||(a[num]>4))) || (num==1&&a[num]>7);
puts(flag?"Mario_wins!\n":"Wario_wins!\n");
}
return 0;
}
【BZOJ】2172: Mario填格子的更多相关文章
- ACM_填格子
填格子 Time Limit: 2000/1000ms (Java/Others) Problem Description: 在一个n*n格子里边已经填了部分大写字母,现在给你个任务:把剩下的格子也填 ...
- 动态规划-填格子问题 Domino and Tromino Tiling
2018-09-01 22:38:19 问题描述: 问题求解: 本题如果是第一看到,应该还是非常棘手的,基本没有什么思路. 不妨先从一种简化的版本来考虑.如果仅有一种砖块,那么,填充的方式如下.
- 填格子3*N的方框使用2*1的矩形进行填充
考虑每个位置的前一个状态 可以发现有 我们分别给他们编号 假设 现在填充到了i+1行,我们可以发现从i行可以通过填充转到i+1行的状态 第i行第j列表示 可以 从上一个转态 j 可以到达这个状态的j ...
- NOI模拟题4 Problem C: 填格子(board)
Solution 首先我们要有敏锐的直觉: 我们将每一列中不选哪种颜色看作是一个序列, 则我们发现这个序列要求相邻两位的颜色不同. 我们还发现, 一个这样的序列对应两种不同的合法的棋盘, 因此统计合法 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- 使用GridVIew显示Gantt(甘特图),动态增减列
说明:本例是做了工厂的排机报表 一.根据查询日期初始化GridView列 private void IniGridView(DateTime p_DateS,DateTime p_DateE) { / ...
- [日常训练]常州集训day7
T1 Description 给定一个序列,初始为空.依次将$1-n$插入序列,其中$i$插到当前第$a_i$个数的右边($a_i=0$表示插到序列最左边).求最终序列. Input 第一行一个整数$ ...
- POJ 1020 Anniversary Cake(DFS)
Anniversary Cake Time Limit: 1000MSMemory Limit: 10000KB64bit IO Format: %I64d & %I64u Submit St ...
- BZOJ2303 APIO2011方格染色(并查集)
比较难想到的是将题目中的要求看做异或.那么有ai,j^ai+1,j^ai,j+1^ai+1,j+1=1.瞎化一化可以大胆猜想得到a1,1^a1,j^ai,1^ai,j=(i-1)*(j-1)& ...
随机推荐
- dblink
drop database link "STANDARD"; drop database link "CSPS" --创建dblink create dat ...
- HDU1899 Sum the K-th's(树状数组)
枚举,每次增加点,删除点 #include<cstdio> #include<iostream> #include<cstdlib> #include<cst ...
- golang debug with LiteIDE
golang 的调试比较麻烦,debug stop into 无法跳转到自己写的代码,但是能够跳转到系统提供的代码. 以下是简单的测试代码: package main import ( "f ...
- C# 使用 NPOI 库读写 Excel 文件
NPOI 是开源的 POI 项目的.NET版,可以用来读写Excel,Word,PPT文件.在处理Excel文件上,NPOI 可以同时兼容 xls 和 xlsx.官网提供了一份 Examples,给出 ...
- android在代码中四种设置控件(以及TextView的文字颜色)背景颜色的方法
http://blog.csdn.net/fth826595345/article/details/9208771 主题 TextView 转载请注明出处: http://blog.csdn.ne ...
- 一个简单的Object Hook的例子(win7 32bit)
Object Hook简单的来说就是Hook对象,这里拿看雪上的一个例子,因为是在win7 32位上的,有些地方做了些修改. _OBJECT_HEADER: kd> dt _OBJECT_HEA ...
- Linux内核system_call中断处理过程
“平安的祝福 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ” men ...
- Jmeter之JDBC请求(四)
我们常用的Jmeter中的功能又HTTP请求.JDBC Request.SOAP/XML -RPC Request,这3个请求, 现在就为大家介绍下 什么是JDBC请求 首先,大家右键点击“测试计划” ...
- 解决android expandablelistview 里面嵌入gridview行数据重复问题
最近做了一个“csdn专家博客App” 当然了是android版本,在专家浏览页面,我才用了expandablelistview 组件来显示专家分类,每个分类点击之后可以显示专家的头像和名字. 很简单 ...
- Liferay 6.2 改造系列之十三:修改用户编辑页面表单内容
为简化用户编辑,删除无用内容: 在/portal-master/portal-impl/src/portal.properties文件中,有如下配置: # # Input a list of sect ...