Codeforces Round #654 (Div. 2) 题解

Problem B

题目

A competitive eater, Alice is scheduling some practices for an eating contest on a magical calendar. The calendar is unusual because a week contains not necessarily 7 days!

In detail, she can choose any integer k which satisfies 1≤k≤r, and set k days as the number of days in a week.

Alice is going to paint some n consecutive days on this calendar. On this calendar, dates are written from the left cell to the right cell in a week. If a date reaches the last day of a week, the next day's cell is the leftmost cell in the next (under) row.

She wants to make all of the painted cells to be connected by side. It means, that for any two painted cells there should exist at least one sequence of painted cells, started in one of these cells, and ended in another, such that any two consecutive cells in this sequence are connected by side.

Alice is considering the shape of the painted cells. Two shapes are the same if there exists a way to make them exactly overlapped using only parallel moves, parallel to the calendar's sides.

For example, in the picture, a week has 4 days and Alice paints 5 consecutive days. [1] and [2] are different shapes, but [1] and [3] are equal shapes.

Alice wants to know how many possible shapes exists if she set how many days a week has and choose consecutive n days and paints them in calendar started in one of the days of the week. As was said before, she considers only shapes, there all cells are connected by side.

Input
The input consists of multiple test cases. The first line contains a single integer t (1≤t≤1000) — the number of test cases. Next t lines contain descriptions of test cases.

For each test case, the only line contains two integers n, r (1≤n≤109,1≤r≤109).

Output
For each test case, print a single integer — the answer to the problem.

Please note, that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language.

思路

我们只需要讨论n和r的大小就可以了,如果n>r那么,答案就是从一开始到r的公差为1的等差数列的和,反之则是1到n-1的等差数列和+1,因为从那之后一行就可以装下,所以形状相同。

Problem D

题目

A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!

You are given integers n,k. Construct a grid A with size n×n consisting of integers 0 and 1. The very important condition should be satisfied: the sum of all elements in the grid is exactly k. In other words, the number of 1 in the grid is equal to k.

Let's define:

Ai,j as the integer in the i-th row and the j-th column.
Ri=Ai,1+Ai,2+...+Ai,n (for all 1≤i≤n).
Cj=A1,j+A2,j+...+An,j (for all 1≤j≤n).
In other words, Ri are row sums and Cj are column sums of the grid A.
For the grid A let's define the value f(A)=(max(R)−min(R))2+(max(C)−min(C))2 (here for an integer sequence X we define max(X) as the maximum value in X and min(X) as the minimum value in X).
Find any grid A, which satisfies the following condition. Among such grids find any, for which the value f(A) is the minimum possible. Among such tables, you can find any.

Input
The input consists of multiple test cases. The first line contains a single integer t (1≤t≤100) — the number of test cases. Next t lines contain descriptions of test cases.

For each test case the only line contains two integers n, k (1≤n≤300,0≤k≤n2).

It is guaranteed that the sum of n2 for all test cases does not exceed 105.

Output
For each test case, firstly print the minimum possible value of f(A) among all tables, for which the condition is satisfied.

After that, print n lines contain n characters each. The j-th character in the i-th line should be equal to Ai,j.

If there are multiple answers you can print any.

思路

一眼出策略的题目,肯定放对角线为优,那么我们把可以被n整除的部分全部放下,剩下一行一个,就是这个代码实现可能有点搞。

代码实现

#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
#define rep(i,f_start,f_end) for (int i=f_start;i<=f_end;++i)
#define per(i,n,a) for (int i=n;i>=a;i--)
#define MT(x,i) memset(x,i,sizeof(x) )
#define rev(i,start,end) for (int i=start;i<end;i++)
#define inf 0x3f3f3f3f
#define mp(x,y) make_pair(x,y)
#define lowbit(x) (x&-x)
#define MOD 1000000007
#define exp 1e-8
#define N 1000005 
#define fi first 
#define se second
#define pb push_back
typedef long long ll;
typedef vector <int> VI;
typedef pair<int ,int> PII;
typedef pair<int ,PII> PIII;
ll gcd (ll a,ll b) {return b?gcd (b,a%b):a; }
inline int read() {
    char ch=getchar(); int x=0, f=1;
    while(ch<'0'||ch>'9') {
        if(ch=='-') f=-1;
        ch=getchar();
    } while('0'<=ch&&ch<='9') {
        x=x*10+ch-'0';
        ch=getchar();
    } return x*f;
}

const int maxn=1e3+10;
int maze[maxn][maxn];
int a[maxn];
int main () {
    int t;
    cin>>t;
    while (t--) {
        int n,k;
        cin>>n>>k;
        MT (maze,0);
        int y=k%n;
        int c=k/n;
        if (y==0) cout<<0<<endl;
        else cout<<2<<endl;
        rev (i,0,y) 
         rev (j,i,i+c+1) {
            maze[i][j%n]=1;
         }
        rev (i,y,n) 
         rev (j,i,i+c) {
             maze[i][j%n]=1;
         }
        rev (i,0,n) {
         rev (j,0,n) {
             cout<<maze[i][j];
         }
         cout<<endl;
        }

    }
    return 0;
}
原文地址:https://www.cnblogs.com/hhlya/p/13561003.html