K - *Restoring Painting(思维)

探讨一个3x3九宫格谜题,已知四个角落的数值,需找出所有可能的九宫格组合,使得每个2x2子区域内的数字之和相等。文章提供了解题思路及代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Vasya works as a watchman in the gallery. Unfortunately, one of the most expensive paintings was stolen while he was on duty. He doesn't want to be fired, so he has to quickly restore the painting. He remembers some facts about it.

  • The painting is a square 3 × 3, each cell contains a single integer from 1 to n, and different cells may contain either different or equal integers.
  • The sum of integers in each of four squares 2 × 2 is equal to the sum of integers in the top left square 2 × 2.
  • Four elements abc and d are known and are located as shown on the picture below.

Help Vasya find out the number of distinct squares the satisfy all the conditions above. Note, that this number may be equal to 0, meaning Vasya remembers something wrong.

Two squares are considered to be different, if there exists a cell that contains two different integers in different squares.

Input

The first line of the input contains five integers nabc and d (1 ≤ n ≤ 100 0001 ≤ a, b, c, d ≤ n) — maximum possible value of an integer in the cell and four integers that Vasya remembers.

Output

Print one integer — the number of distinct valid squares.

Example
Input
2 1 1 1 2
Output
2
Input
3 3 1 2 3
Output
6
Note

Below are all the possible paintings for the first sample.  

In the second sample, only paintings displayed below satisfy all the rules.      

题意:有一个3*3的九宫格,给你a,b,c,d四个数,其余不知道,在这个九宫格中,每一个田字格的中的数字和是相同的,每个未知的格子中数字范围是1~n;问你有多少种这样的九宫格;

思路:由于a,b,c,d的值已经给你了,那么我们枚举左上角的格子的值,就可以算出其余三个角的值,如果这三个边角的格子的值在1~n之间,那么就符合要求;至于正中间的那个格子无所谓,从1~n的值中其余四角的情况都相同,所以只要遍历一遍左上角的值求出情况总数再乘以n即可;

下面附上代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{	
	long long n,a,b,c,d,q,w,e;
	while(~scanf("%lld %lld %lld %lld %lld",&n,&a,&b,&c,&d))
	{
		long long sum=0;
		for(long long i=1;i<=n;i++)
		{	
				int flag1=0,flag2=0,flag3=0;
				q=i+b-c;
				if(q>0&&q<=n) flag1=1;
				w=i+a-d;
				if(w>0&&w<=n) flag2=1;
				e=i+a+b-d-c;
				if(e>0&&e<=n) flag3=1;
				if(flag1&&flag2&&flag3)
					sum++;
			}
			printf("%lld\n",sum*n);
		}
	return 0;
}


# Web方向实验报告 - ### 问题描述: 在传统的Web应用中,实现用户自动登录功能通常需要在每个Servlet中编写重复的Cookie校验代码,导致代码冗余和维护困难。本次实验通过Filter(过滤器)拦截请求,统一处理Cookie校验,实现自动登录功能。核心目标包括: 1. 通过`AutoLoginFilter`拦截请求,自动解析Cookie中的用户信息并完成登录。 2. 减少Servlet中的重复校验代码,提升代码复用性和可维护性。 *** - ### 实验设计: #### 1. 整体架构 - **User实体类**:封装用户信息(用户名、密码)。 - **LoginServlet**:处理登录请求,校验用户名密码,生成自动登录Cookie。 - **LogoutServlet**:注销用户,清除Session和Cookie。 - **AutoLoginFilter**:拦截请求,校验Cookie并自动登录。 - **JSP页面**:`login.jsp`(登录页)、`index.jsp`(首页)。 #### 2. 关键模块设计 - **Cookie生成与解析**- 登录成功时,将用户名和密码拼接为`username#password`存入Cookie。 - Filter解析Cookie时,按分隔符`#`拆分并验证用户信息。 - **过滤器排除规则**- 静态资源(`.jsp`、`.css`、`.js`、`.png`)和登录请求(`/LoginServlet`)不经过过滤器。 *** - ### 实验实现: #### 1. User实体类 - **作用**:封装用户数据,用于Session存储和业务逻辑传递。 - **关键代码**: ```java public class User { private String username; private String password; } ``` #### 2. LoginServlet - **核心逻辑**- 校验用户名密码(硬编码为`ZYR/123456`)。 - 若勾选“自动登录”,生成Cookie并
最新发布
04-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值