2021牛客暑期多校训练营3 F 24dian
先导
但是上述博客说的有6种情况,其实这道题对于非24点有8种
比如 a/b+c/d=m or a/b-c/d=m
🎈24dian
题意
求4个数等于m的必出现分数例子(1,2,3都为0)
样例
输入
4 24
输出
16
1 3 4 6
1 4 5 6
1 5 5 5
1 6 6 8
1 8 12 12
2 2 11 11
2 2 13 13
2 3 5 12
2 4 10 10
2 5 5 10
2 7 7 10
3 3 7 7
3 3 8 8
4 4 7 7
5 5 7 11
5 7 7 11
思路
按照那个博客的思路来,再加上另外两种情况,就行了
AC代码
#include <bits/stdc++.h>
#include<complex>
#define ull unsigned long long
#define ll long long
using namespace std;
const int N=3e7+10;
int n,m;
bool ok2(int a, int b)
{
if (a + b == m || a - b == m || a*b == m || a != 0 && a == m * b)
{
return true;
}
return false;
}
bool ok3(int a, int b, int c)
{
if (b == 0)
{
if (ok2(a, c))return true;
return false;
}
if (c == 0)
{
if (ok2(a, b))return true;
return false;
}
if (ok2((a + b), c) || ok2((a - b), c) || ok2((a * b), c))return true;
if (a%b == 0 && ok2(a / b, c))return true;
if (ok2(a, b + c) || ok2(a, b - c) || ok2(a, b * c))return true;
if (b%c == 0 && ok2(a, b / c))return true;
return false;
}
bool ok(int a, int b, int c, int d)
{
if (ok3(a + b, c, d) || ok3(a - b, c, d) || ok3(a * b, c, d))return true;
if (a%b == 0 && ok3(a / b, c, d))return true;
if (ok3(a, b + c, d) || ok3(a, b - c, d) || ok3(a, b * c, d))return true;
if (b%c == 0 && ok3(a, b / c, d))return true;
if (ok3(a, b, c + d) || ok3(a, b, c - d) || ok3(a, b, c * d))return true;
if (c%d == 0 && ok3(a, b, c / d))return true;
return false;
}
bool ok1(int a, int b, int c, int d)
{
if (ok(a, b, c, d) || ok(a, c, b, d) || ok(b, a, c, d) || ok(b, c, a, d) || ok(c, a, b, d) || ok(c, b, a, d))return true;
return false;
}
bool pan(int a, int b, int c, int d){
// cout<<(a*c - b)*d<<" "<<(b - a*c)*d<<" "<<(a*c + b)*d<<" "<<m*c<<endl;
//cout<<c*d<<" "<<m * (a*c - b)<<" "<<m * (b - a*c)<<" "<<m * (a*c + b)<<endl;
if ((a*c - b)*d == m * c || (b - a*c)*d == m * c || (a*c + b)*d == m * c) return true;
if (c*d == m * (a*c - b) || c*d == m * (b - a*c) || c*d == m * (a*c + b)) return true;
return false;
}
struct node {
int a[4];
friend bool operator<(const node a,const node b){
for(int i=0;i<=3;i++){
if(a.a[i]!=b.a[i]){
return a.a[i]<b.a[i];
}
}
}
}a[1000];
int main() {
// m=4;
// pan(6,13,6,11);
// pan(13,6,11,6);
while(~scanf("%d%d",&n,&m)){
if(n==1 || n==2 || n==3 || m>=28561){
printf("0\n");
}
else{
map<string,int>mp;
int cnt=0;
for(int i=1;i<=13;i++){
for(int j=1;j<=13;j++){
for(int k=1;k<=13;k++){
for(int z=1;z<=13;z++){
if(ok1(i, j, k, z) || ok1( j, k, z,i) || ok1( k, z,i,j) || ok1( z,i, j, k)){
continue;
}
else{
//cout<<i<<" "<<j<<" "<<k<<" "<<z<<endl;
if(j%k && pan(i,j,k,z) ||(i*z+j*k==m*j*z && i%j && k%z)||(i*z-j*k==m*j*z && i%j && k%z)){
// cout<<i<<" "<<j<<" "<<k<<" "<<z<<endl;
int b[4];b[0]=i;b[1]=j;b[2]=k;b[3]=z;
sort(b,b+4);
string c="";
c+=(b[0]+'a');c+='+';
c+=(b[1]+'a');c+='+';
c+=(b[2]+'a');c+='+';
c+=(b[3]+'a');
if(mp[c]==0){
a[cnt].a[0]=b[0];a[cnt].a[1]=b[1];a[cnt].a[2]=b[2];a[cnt++].a[3]=b[3];
mp[c]=1;
}
}
}
}
}
}
}
sort(a,a+cnt);
printf("%d\n",cnt);
for(int i=0;i<cnt;i++){
printf("%d %d %d %d\n",a[i].a[0],a[i].a[1],a[i].a[2],a[i].a[3]);
}
}
}
return 0;
}
/*
4 1
20
1 5 8 10
1 5 10 12
1 6 10 12
3 3 4 7
3 3 7 10
3 3 8 11
3 3 10 13
3 8 11 11
3 10 13 13
4 4 5 9
4 4 9 13
4 5 5 9
4 7 11 11
4 9 13 13
5 5 6 11
5 5 8 13
5 6 6 11
5 8 13 13
6 6 7 13
6 7 7 13
*/