[SinGuLaRiTy] 百度之星程序设计大赛 初赛

时间:2022-10-01 09:05:21

小C的倍数问题

 Time Limit: 2000/1000 MS (Java/Others)   Memory Limit: 32768/32768 K (Java/Others)

Problem Description

根据小学数学的知识,我们知道一个正整数x是3的倍数的条件是x每一位加起来的和是3的倍数。反之,如果一个数每一位加起来是3的倍数,则这个数肯定是3的倍数。

现在给定进制P,求有多少个B满足P进制下,一个正整数是B的倍数的充分必要条件是每一位加起来的和是B的倍数。

Input

第一行一个正整数T表示数据组数(1<=T<=20)。

接下来T行,每行一个正整数P(2 < P < 1e9),表示一组询问。

Output

对于每组数据输出一行,每一行一个数表示答案。

Sample Input

1

10

Sample Output

3

Code

//小C的倍数问题
#include<cstring>
#include
<cmath>
#include
<algorithm>
#include
<iostream>
#include
<cstdio>
#include
<cstdlib>

using namespace std;

int T;

int main()
{
cin
>>T;
for(int i=1;i<=T;i++)
{
int n,cnt=0;
scanf(
"%d",&n);
n
=n-1;
for(int i=1;i*i<=n;i++)
{
if(n%i==0)
cnt
+=2;
if(i*i==n)
cnt
--;
}
printf(
"%d\n",cnt);
}
return 0;
}

度度熊的01世界

 Time Limit: 2000/1000 MS (Java/Others)   Memory Limit: 32768/32768 K (Java/Others)

Problem Description

度度熊是一个喜欢计算机的孩子,在计算机的世界中,所有事物实际上都只由0和1组成。

现在给你一个n*m的图像,你需要分辨他究竟是0,还是1,或者两者均不是。

图像0的定义:存在1字符且1字符只能是由一个连通块组成,存在且仅存在一个由0字符组成的连通块完全被1所包围。

图像1的定义:存在1字符且1字符只能是由一个连通块组成,不存在任何0字符组成的连通块被1所完全包围。

连通的含义是,只要连续两个方块有公共边,就看做是连通。

完全包围的意思是,该连通块不与边界相接触。

Input

本题包含若干组测试数据。 每组测试数据包含: 第一行两个整数n,m表示图像的长与宽。 接下来n行m列将会是只有01组成的字符画。

满足1<=n,m<=100

Output

如果这个图是1的话,输出1;如果是0的话,输出0,都不是输出-1。

Sample Input

32 32
00000000000000000000000000000000
00000000000111111110000000000000
00000000001111111111100000000000
00000000001111111111110000000000
00000000011111111111111000000000
00000000011111100011111000000000
00000000111110000001111000000000
00000000111110000001111100000000
00000000111110000000111110000000
00000001111110000000111110000000
00000001111110000000011111000000
00000001111110000000001111000000
00000001111110000000001111100000
00000001111100000000001111000000
00000001111000000000001111000000
00000001111000000000001111000000
00000001111000000000000111000000
00000000111100000000000111000000
00000000111100000000000111000000
00000000111100000000000111000000
00000001111000000000011110000000
00000001111000000000011110000000
00000000111000000000011110000000
00000000111110000011111110000000
00000000111110001111111100000000
00000000111111111111111000000000
00000000011111111111111000000000
00000000111111111111100000000000
00000000011111111111000000000000
00000000001111111000000000000000
00000000001111100000000000000000
00000000000000000000000000000000
32 32
00000000000000000000000000000000
00000000000000001111110000000000
00000000000000001111111000000000
00000000000000011111111000000000
00000000000000111111111000000000
00000000000000011111111000000000
00000000000000011111111000000000
00000000000000111111110000000000
00000000000000111111100000000000
00000000000001111111100000000000
00000000000001111111110000000000
00000000000001111111110000000000
00000000000001111111100000000000
00000000000011111110000000000000
00000000011111111110000000000000
00000001111111111111000000000000
00000011111111111111000000000000
00000011111111111111000000000000
00000011111111111110000000000000
00000000001111111111000000000000
00000000000000111111000000000000
00000000000001111111000000000000
00000000000111111110000000000000
00000000000011111111000000000000
00000000000011111111000000000000
00000000000011111111100000000000
00000000000011111111100000000000
00000000000000111111110000000000
00000000000000001111111111000000
00000000000000001111111111000000
00000000000000000111111111000000
00000000000000000000000000000000
3 3
101
101
011

Sample Output

0
1
-1

Code

#include<cstring>
#include
<cstdio>
#include
<cmath>
#include
<algorithm>
#include
<iostream>
#include
<cstdlib>

#define MAXN 110

#define zero_one_zero 10
#define one_zero_one 20
#define all_zero 30
#define ELSE 90

using namespace std;

char block[MAXN][MAXN];
int docking[MAXN][MAXN];
int d_cnt=1;
int Part=0;//1.2.3......
int status[MAXN];
int n,m;
int flag=true;

bool judge()
{
for(int i=2;i<=n;i++)
{
if(status[i]==all_zero||status[i-1]==all_zero)
continue;
if(status[i]==zero_one_zero&&status[i-1]==zero_one_zero)
if((docking[i][2]<docking[i-1][1])||(docking[i][1]>docking[i-1][2]))
return false;
if(status[i]==one_zero_one&&status[i-1]==one_zero_one)
if(((docking[i][2]<docking[i-1][1])||(docking[i][1]>docking[i-1][2]))||((docking[i][4]<docking[i-1][3])||(docking[i][3]>docking[i-1][4])))
return false;
if(status[i]==zero_one_zero&&status[i-1]==one_zero_one)
if(docking[i][1]>docking[i-1][2]||docking[i][2]<docking[i-1][3])
return false;
if(status[i]==one_zero_one&&status[i-1]==zero_one_zero)
if(docking[i-1][1]>docking[i][2]||docking[i-1][2]<docking[i][3])
return false;
}
return true;
}

bool judge_11()
{
for(int i=1;i<=n;i++)
{
if(status[i]==one_zero_one)
return false;
}
return true;
}

bool judge_12()
{
for(int i=1;i<=n;i++)
{
if(status[i]!=all_zero)
return false;
}
return true;
}

int main()
{
while(scanf("%d%d",&n,&m)==2&&(n+m!=0))
{
memset(docking,
0,sizeof(docking));
memset(block,
0,sizeof(block));
memset(status,
0,sizeof(status));
d_cnt
=1;
Part
=0;
flag
=true;
for(int i=1;i<=n;i++)
{
scanf(
"%s",block[i]+1);
}

for(int i=1;i<=n;i++)
{
int in_cnt=0,out_cnt=0;
d_cnt
=1;
for(int j=1;j<=m;j++)
{
if((block[i][j]=='0'&&block[i][j+1]=='1')||(block[i][j]=='1'&&j==1))
{
in_cnt
+=1;
if(j!=1)
docking[i][d_cnt
++]=j;
else
docking[i][d_cnt
++]=0;
}
if((block[i][j]=='1'&&block[i][j+1]=='0')||(block[i][j]=='1'&&j==m))
{
out_cnt
+=1;
docking[i][d_cnt
++]=j;
}
}
if(in_cnt==1&&out_cnt==1)//010
{
status[i]
=zero_one_zero;
continue;
}
else if(in_cnt==2&&out_cnt==2)//101
{
status[i]
=one_zero_one;
continue;
}
else
{
bool if_all_zero=true;
for(int j=1;j<=m;j++)
{
if(block[i][j]!='0')
{
if_all_zero
=false;
break;
}
}
if(!if_all_zero)
status[i]
=ELSE;
else
status[i]
=all_zero;
}
}

int enter=0,exit=0;
for(int i=1;i<=n;i++)
{
if(status[i]==ELSE)
{
cout
<<"-1"<<endl;
flag
=false;
break;
}
if(status[i]==zero_one_zero&&status[i+1]==one_zero_one)
{
enter
+=1;
}
}
if(enter==1)
{
if(judge()==true)
{
cout
<<"0"<<endl;
continue;
}
else
{
cout
<<"-1"<<endl;
continue;
}
}
if(enter==0)
{
if(judge()==true&&judge_11()==true&&judge_12()==false)
{
cout
<<"1"<<endl;
continue;
}
else
{
cout
<<"-1"<<endl;
continue;
}
}
if(flag==false)
continue;
if(enter>1)
{
cout
<<"-1"<<endl;
continue;
}
}
return 0;
}