Description

Bipartite Number是这样的一个正整数,他只能由两段相同的数组成,如44444411,10000000, 5555556,41,而4444114,44444则不是。
现给你一个N,让你找到最小的Bipartite NumberX,使得X=NK(K是正整数,K>1),由于答案X可能很大,你需将其缩写转换为4个数字输出,即(高位数字长度,高位数字,低位数字长度,低位数字),如44444411为6 4 2 1,88888888888800000为12 8 5 0,N<=1e5,T<=20

从小到大枚举位数,再枚举最高位,用一个表动态记录%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的更多相关文章

  1. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  2. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  3. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  4. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  5. [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 ...

  6. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  7. [LeetCode] Bitwise AND of Numbers Range 数字范围位相与

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  8. [LeetCode] Valid Phone Numbers 验证电话号码

    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...

  9. [LeetCode] Consecutive Numbers 连续的数字

    Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...

随机推荐

  1. HDU5658:CA Loves Palindromic (回文树,求区间本质不同的回文串数)

    CA loves strings, especially loves the palindrome strings. One day he gets a string, he wants to kno ...

  2. 阿里druid数据库连接池配置

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  3. HDU2027:统计元音

    Problem Description 统计每个元音字母在字符串中出现的次数. Input 输入数据首先包括一个整数n,表示测试实例的个数,然后是n行长度不超过100的字符串. Output 对于每个 ...

  4. UVALive-6540 Fibonacci Tree

    #include<bits/stdc++.h> using namespace std; int n,m; struct edge { int x; int y; int len; }ed ...

  5. 20165313 《Java程序设计》第八周学习总结

    教材学习总结 线程常用方法 1.start() 2.run()定义线程线程对象被调度之后所执行的操作 3.sleep(int millsecond),必须在try-catch语句块中调用sleep方法 ...

  6. Sublime Text怎么快速建立一个html5页面模板

    在编辑器中输入一个半角英文的感叹号(!),然后按下TAB键

  7. 在CentOS 6上使用 AWStats 分析 httpd 和 Tomcat 日志

    准备工作: Awstats 是由perl语言编写的,所以要首先准备好awstats的运行环境.# yum install –y perl*   Apache 一.首先,要安装apache服务器,并且启 ...

  8. pandas读取MySql/SqlServer数据 (转)

    在 Anacondas环境中,conda install pymssql ,一直报包冲突,所以采用先在 https://www.lfd.uci.edu/~gohlke/pythonlibs/#nump ...

  9. java JVM JRE JDK三者之间的关系

    JDK在包含JRE之外,提供了开发Java应用的各种工具,比如编译器和调试器. JRE包括JVM和JAVA核心类库和支持文件,是Java的运行平台. JVM是JRE的一部分,Java虚拟机的主要工作是 ...

  10. HanLP极致简繁转换详细讲解

    HanLP极致简繁转换详细讲解 作者: hankcs(大快高级研究员 hanlp项目负责人) 谈起简繁转换,许多人以为是小意思,按字转换就行了.事实上,汉语历史悠久,地域复杂,发展至今在字符级别存在“ ...