bzoj2909: Bipartite Numbers
Description
从小到大枚举位数,再枚举最高位,用一个表动态记录%N=a的形如(10^c-1)/9*b(c<当前位数,1<=|b|<=9)的数中最小和次小的一个,以便枚举最低位的值查询是否可行
#include<cstdio>
#include<cstring>
int n,a1,a2,u1,u2,uu1,uu2;
int f[][][],ed[],tk=;
inline int fix(int x){return x+(x>>&n);}
void ins(int p1,int p2,int v){
if(ed[p1]!=tk){
ed[p1]=tk;
memset(f[p1],-,sizeof(f[p1]));
}
f[p1][p2][]=f[p1][p2][];
f[p1][p2][]=v;
}
int cal(int v1,int v2){
v2-=;
return (v2>?v1:-v1)*+v2;
}
void chk(int c1,int c2){
if(~c1&&(a1==-||cal(c1,c2)<cal(a1,a2)))a1=c1,a2=c2;
}
void find(int a,int b){
if(ed[a]==tk)chk(f[a][b][],b);
}
void findx(int a,int b,int c){
if(ed[a]==tk){
if(c==uu2){
if(f[a][b][]!=u2)chk(f[a][b][],b);
if(f[a][b][]!=u2)chk(f[a][b][],b);
}else chk(f[a][b][],b);
}
}
void chk(){
u1=-;
int ss[],sp=,p1,p2;
for(int x=n;x;ss[++sp]=x%,x/=);
for(p1=sp;p1>&&ss[p1]==ss[sp];--p1);
for(p2=p1-;p2>&&ss[p2]==ss[p1-];--p2);
if(p1&&!p2)u1=sp,u2=p1,uu1=ss[sp],uu2=ss[p1];
}
void cal(){
if(n==){
puts("1 1 1 0");
return;
}
++tk;
chk();
a1=-;
for(int i=,v=;;++i,v=(v*+)%n){
for(int a=,z=v;a<=;++a,z=fix(z+v-n)){
if(i==u1&&a==uu1){
for(int b=;b<=;++b)if(b!=a)findx(z,+b-a,b);
}else for(int b=;b<=;++b)if(b!=a)find(z,+b-a);
if(a1!=-){
printf("%d %d %d %d\n",i-a1,a,a1,a+a2-);
return;
}
}
for(int a=,z=v;a<=;++a,z=fix(z+v-n)){
ins(fix(-z),+a,i);
ins(z,-a,i);
}
}
}
int main(){
while(scanf("%d",&n)==&&n)cal();
return ;
}
bzoj2909: Bipartite Numbers的更多相关文章
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Bitwise AND of Numbers Range 数字范围位相与
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- [LeetCode] Valid Phone Numbers 验证电话号码
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- [LeetCode] Consecutive Numbers 连续的数字
Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...
随机推荐
- webapi core2.1 Identity.EntityFramework Core进行配置和操作数据 (一)没什么用
https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity?view=aspnetcore-2.1&am ...
- C++学习(二十)(C语言部分)之 函数1
函数 printf 输出的函数 scanf 输入的函数函数是什么 怎么写 是一组一起执行一个任务的语句 一个程序的基本组成单位是函数 只需要知道函数名字和括号里面要填的内容 就可以调用函数 1.如果代 ...
- Redis(二)持久化
Redis持久化,分为RDB方式和AOF方式,它们可以单独使用,也可以混用.Redis默认的是使用RDB方式. 一.RDB方式 1.触发快照的方式 RDB方式是在指定时间间隔内某一时间点的数据集快照. ...
- java 8大数据类型
第一类:逻辑型boolean 第二类:文本型char 1.JAVA中,char占2字节,16位.可在存放汉字 2.char赋值 char a='a'; //任意单个字符,加单引号. char a=' ...
- LeetCode - Rotate String
We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...
- MySQL Innodb Engine -- 文件格式(innodb_file_format)
======================================================== 在InnoDB 1.0.x版本之前,InnoDB 存储引擎提供了 Compact 和 ...
- vernemq 集群 docker-compose 搭建简单试用
vernemq 是一款开源的mqtt broker, 支持cluster 模式部署,而且部署比较简单 以下是一个使用docker-compose 搭建环境的demo 环境准备 docker-compo ...
- Unity API 解析 (陈泉宏著)
1 Application类 2 Camera类 3 GameObject类 4 HideFlags类 5 Mathf类 6 Matrix4x4类 7 Object类 8 Quaternion类 9 ...
- DevExpress控件使用方法:第二篇 barManager
标题栏 一.Bars 1. 把BarManager组件添加到窗体中后,会自动创建三个空的 bars: 主菜单(通常位于窗体顶部).顶部工具栏.窗体底部的状态栏. 2. 隐藏左侧的竖线和右边的箭 ...
- MySQL 遇到错误集锦
MySQL 规定:varchar必须指定长度,否则报错:ERROR 1064 (42000) 只有一列时,primary key 直接写在这一列的后面: 没有定义主键时,提示错误:ERROR 1075 ...