求出最大公约数即可
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<stack>
#include<functional>
using namespace std;
const int maxn = 100010;
int num[maxn];
int gcd(int a,int b){
return b==0?a:gcd(b,a%b);
}
int main()
{
int n,k;
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%d",&num[i]);
}
if(n==1){
printf("%d\n",num[1]);
}
else {
int ans=gcd(num[1],num[2]);
for(int i=3;i<=n;++i){
ans=gcd(ans,num[i]);
}
printf("%d\n",ans*n);
}
return 0;
}
Fox Ciel has a board with n rows and n columns. So, the board consists of n × n cells. Each cell contains either a symbol '.', or a symbol '#'.
A cross on the board is a connected set of exactly five cells of the board that looks like a cross. The picture below shows how it looks.
Ciel wants to draw several (may be zero) crosses on the board. Each cross must cover exactly five cells with symbols '#', and any cell with symbol '#' must belong to some cross. No two crosses can share a cell.
Please, tell Ciel if she can draw the crosses in the described way.
The first line contains an integer n (3 ≤ n ≤ 100) — the size of the board.
Each of the next n lines describes one row of the board. The i-th line describes the i-th row of the board and consists of n characters. Each character is either a symbol '.', or a symbol '#'.
Output a single line with "YES" if Ciel can draw the crosses in the described way. Otherwise output a single line with "NO".
5 .#... ####. .#### ...#. .....
YES
4 #### #### #### ####
NO
6 .#.... ####.. .####. .#.##. ###### .#..#.
YES
6 .#..#. ###### .####. .####. ###### .#..#.
NO
3 ... ... ...
YES
In example 1, you can draw two crosses. The picture below shows what they look like.
In example 2, the board contains 16 cells with '#', but each cross contains 5. Since 16 is not a multiple of 5, so it's impossible to cover all.
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<stack>
#include<functional>
using namespace std;
const int maxn = 1010;
bool vis[maxn][maxn];
char Map[maxn][maxn];
int main()
{
int n;
scanf("%d",&n);
for(int i = 1;i <=n;++i){
scanf("%s",Map[i]+1);
}
memset(vis,false,sizeof(vis));
bool ok = true;
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
if(Map[i][j]=='#'&&!vis[i][j]){
if(i+2>n||j+1>n||j-1<1)ok=false;
else if(vis[i+1][j]||vis[i+2][j]||vis[i+1][j-1]||vis[i+1][j+1]||Map[i+1][j]!='#'||Map[i+2][j]!='#'||Map[i+1][j-1]!='#'||Map[i+1][j+1]!='#'){
ok=false;
}
else {
vis[i][j]=vis[i+1][j]=vis[i+2][j]=vis[i+1][j-1]=vis[i+1][j+1]=true;
}
}
}
if(!ok)break;
}
if(ok){
printf("YES\n");
}
else {
printf("NO\n");
}
return 0;
}
Fox Ciel has n boxes in her room. They have the same size and weight, but they might have different strength. The i-th box can hold at most xi boxes on its top (we'll call xi the strength of the box).
Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example, imagine Ciel has three boxes: the first has strength 2, the second has strength 1 and the third has strength 1. She cannot put the second and the third box simultaneously directly on the top of the first one. But she can put the second box directly on the top of the first one, and then the third box directly on the top of the second one. We will call such a construction of boxes a pile.
Fox Ciel wants to construct piles from all the boxes. Each pile will contain some boxes from top to bottom, and there cannot be more thanxi boxes on the top of i-th box. What is the minimal number of piles she needs to construct?
The first line contains an integer n (1 ≤ n ≤ 100). The next line contains n integers x1, x2, ..., xn (0 ≤ xi ≤ 100).
Output a single integer — the minimal possible number of piles.
3 0 0 10
2
5 0 1 2 3 4
1
4 0 0 0 0
4
9 0 1 0 2 0 1 1 2 10
3
In example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2.
In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom).
二分
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<stack>
#include<functional>
using namespace std;
const int maxn = 110;
int temp[maxn];
int num[maxn],n;
bool judge(int mid){
for(int i=1;i<=mid;++i){
temp[i]=num[i];
}
for(int i=mid+1;i<=n;++i){
int pos=i%mid;if(pos==0)pos=mid;
if(temp[pos]-1<0)return false;
temp[pos]=min(temp[pos]-1,num[i]);
}
return true;
}
bool cmp(int a,int b){
return a>b;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%d",&num[i]);
}
sort(num+1,num+n+1,cmp);
int left=1,right=n,ans;
while(left<=right){
int mid=(left+right)>>1;
if(judge(mid)){
ans=mid;
right=mid-1;
}
else {
left=mid+1;
}
}
printf("%d",ans);
return 0;
}