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. +----+-----+ | ...
随机推荐
- 相对和绝对路径 mkdir cd rm 等命令
1. 绝对路径和相对路径 个人理解: 绝对路径-----即从根目录开始一直到你需要找的文件或目录的路径 (即任何情况下都以根目录为起点) 相对路径------即从当前目录开始一直找到你需要找的 ...
- <<操作,&0xff以及|的巧妙运用(以POJ3523---The Morning after Halloween(UVa 1601)为例)
<<表示左移,如a<<1表示将a的二进制左移一位,加一个0,&0xff表示取最后8个字节,如a&0xff表示取a表示的二进制中最后8个数字组成一个新的二进制数, ...
- 安装Ubuntu16.04与windows10双系统后,如何修改启动默认设置
在安装了Ubuntu16.04系统之后,系统会默认自启动Ubuntu16.04,而我们大多数情况下可能都在使用windows系统,不修改默认设置,不经意间便会启动了Ubuntu16.04,通过我的经历 ...
- 安装,配置,启动FTP,SSH,NFS服务
1.安装,配置,启动FTP服务 sudo apt-get install vsftpd 修改vsftpd的配置文件/etx/vsftpd/.config,将下面几行前面的“#”去掉 #local_en ...
- 《DSP using MATLAB》Problem 5.4
代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...
- Unity3D中常用的数据结构 学习
Array 1.数组存储在连续的内存上. 2.数组的元素都是相同类型或者类型的衍生类型 3.数组可以直接通过下标访问 ArrayList 1.不必在声明ArrayList时指定它的长度,这是由于Arr ...
- DOS命令之at命令详解
AT命令是Windows XP中内置的命令,它也可以媲美Windows中的“计划任务”,而且在计划的安排.任务的管理.工作事务的处理方面,AT命令具有更强大更神通的功能.AT命令可在指定时间和日期.在 ...
- spring+mybaits xml配置解析----转
一.项目中spring+mybaits xml配置解析 一般我们会在datasource.xml中进行如下配置,但是其中每个配置项原理和用途是什么,并不是那么清楚,如果不清楚的话,在使用时候就很有可能 ...
- 【转】Android AlertDialog自定义布局
原文网址:https://blog.csdn.net/u010694658/article/details/53022294 由于开发中经常使用弹框,然而系统自带的弹框太局限,也不太美观,经常不能满足 ...
- streaming简介
mapreduce和hdfs采用java实现,默认提供java编程接口 streaming框架允许任何程序语言实现的程序在hadoop mapreduce中使用 streaming方便已有的程序向ha ...