E:
题目描述:
Niuniu wants to tour the cities in Moe country. Moe country has a total of n*m cities. The positions of the cities form a grid with n rows and m columns. We represent the city in row x and column y as (x,y). (1 ≤ x ≤ n,1 ≤ y ≤ m) There are bidirectional railways between adjacent cities. (x1,y1) and (x2,y2) are called adjacent if and only if |x1-x2|+|y1-y2|=1. There are also K bidirectional air lines between K pairs of cities. It takes Niuniu exactly one day to travel by a single line of railway or airplane. Niuniu starts and ends his tour in (1,1). What is the minimal time Niuniu has to travel between cities so that he can visit every city at least once?
Note that the air line may start and end in the same city.
输入描述:
The first line contains oneinteger T(T≤20), which means the number of test cases.
Each test case has the format as described below.
n m K
ax1 ay1 bx1 by1
ax2 ay2 bx2 by2
…
axK ayK bxK byK
(0 ≤ K ≤ 10. 2 ≤ n,m ≤ 100, 1 ≤ n*m ≤ 100)
There is one bidirectional air line between (axi,ayi) and (bxi,byi). (1 ≤ axi,bxi ≤ n , 1 ≤ ayi,byi ≤ m)
输出描述:
For each test case,print one number in a single line, which is the minimal number of days Niuniu has to travel between cities so that he can visit every city at least once.
示例:
3
2 2 1
1 1 2 2
3 3 1
1 1 3 3
3 3 0
输出:
4
9
10
题意:
在保证经过所有点的情况下找到一条可以回到初始点的路线。其中除了特殊(输入)的路线仅可以上下左右移动
题解:
易知所求路线最短也需要经过所有点,因此肯定大于等于m*n。n,m至少为偶数时一定可以找到nm的路
同理当m,n均为奇数时且无捷径则至少需要m*n+1的路线。由此可以解出该题。
通过代码:
#include<bits/stdc++.h>
#include<string>
#include<queue>
#include<map>
#include<set>
using namespace std;
typedef int ll;
const int N=1e9;
int main()
{
int t,n,m,k,sum=0,flag=0;
int ax1,ay1,bx2,by2;
cin>>t;
while(t--){
cin>>n>>m>>k;
flag=0;
for(int i=0;i<k;i++){
cin>>ax1>>ay1>>bx2>>by2;
if(ax1!=bx2||ay1!=by2){ //不是一个点判断
if((ax1+ay1)%2==0&&(bx2+by2)%2==0){
flag=1;
}
}
}
if(n%2==0||m%2==0){
sum=n*m;
}
else{
if(flag==0){
sum=n*m+1;
}
else{
sum=n*m;
}
}
cout<<sum<<endl;
}
}
G:
题目描述:
Niuniu likes mathematics. He also likes drawing pictures. One day, he was trying to draw a regular polygon with n vertices. He connected every pair of the vertices by a straight line as well. He counted the number of regions inside the polygon after he completed his picture. He was wondering how to calculate the number of regions without the picture. Can you calculate the number of regions modulo 1000000007? It is guaranteed that n is odd.
输入描述:
The only line contains one odd number n(3 ≤ n ≤ 1000000000), which is the number of vertices.
输出描述:
Print a single line with one number, which is the answer modulo 1000000007.
示例 1:
输入:3
输出:1
示例2:
输入:5
输出:11
题意:
输出奇数顶点数多边形可以出现的最多区域个数
知识点:
图论(欧拉公式)
题解:
易知该正多边形顶点位于同一圆上,则满足欧拉公式。
已知欧拉公式:V-E+F=2
V(顶点)= n(N边形的N个顶点) + C(n,4)(在圆上的每四个点确定一个交点)
E(边数)=n(N边形外围的N条边)+C(n,2)(每两个顶点连线确定一条边)+2*C(n,4)(多边形内的交点必然有两条线经过)
由此F(平面内所有区域个数)=E-V+2=C(n,2)+C(n,4)+2
则F1(多边形内部区域个数)=F-n(N边形N条边和圆所围面积个数)-1(圆外)=C(n,2)+C(n,4)-n+1
(图中红色为上述n的部分)
代码:
#include<iostream>
#include<string>
#include<queue>
#include<map>
#include<set>
using namespace std;
typedef long long int ll;
const int N = 1e9;
const int M = 1e9 + 7;
ll n;
ll P_pow(ll a) //逆元
{
ll sum = 1, ans = a, m = M - 2;
while (m)
{
if (m & 1) sum = sum * ans % M;
ans = ans * ans % M;
m >>= 1;
}
return sum;
}
int main()
{
ll F = 0, E, V;
cin >> n;
E = n * (n - 1) % M * P_pow(2);
V = n * (n - 1) % M * (n - 2) % M * (n - 3) % M * P_pow(24) % M;
F = E + V - n + 1;
cout << F % M;
}