山東省第六屆ACM大學生程式設計競賽-Single Round Math(大數除法)
Single Round Math
Time Limit: 1000ms Memory limit: 65536K 有疑問?點這裡^_^
題目描述
Association for Couples Math (ACM) is a non-profit organization which is engaged in helping single people to find his/her other half. As November 11th is “Single Day”, on this day, ACM invites a large group of singles to the party. People round together,
chatting with others, and matching partners.
There are N gentlemen and M ladies in the party, each gentleman should only match with a lady and vice versa. To memorize the Singles Day, ACM decides to divides to divide people into 11 groups, each group should have the same amount of couples and no
people are left without the groups.
Can ACM achieve the goal?
輸入
The first line of the input is a positive integer T. T is the number of test cases followed. Each test case contains two integer N and M (0 ≤ N, M ≤ 101000), which are the amount of gentlemen and ladies.
輸出
For each test case, output “YES” if it is possible to find a way, output “NO” if not.
示例輸入
3 1 1 11 11 22 11
示例輸出
NO YES NO
提示
來源
題目意思:
分別給出男和女的人數,是否能夠分成11組,且每組男女兩兩一對,沒有人是多餘的。
解題思路:
高精度除法。
資料真是大得可怕…10的1000次方…
直接套用的kuangbin大神的模板~然後自己寫了個判斷兩個數是否相等的函式。
注意以下三種情況不滿足的話就是NO:
①男女必須兩兩配對人數要相同;
②男女人數要是11的倍數;
③不能有被剩下的人。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
//compare比較函式:相等返回0,大於返回1,小於返回-1
int compare(string str1,string str2)
{
if(str1.length()>str2.length()) return 1;
else if(str1.length()<str2.length()) return -1;
else return str1.compare(str2);
}
//高精度加法
//只能是兩個正數相加
string add(string str1,string str2)//高精度加法
{
string str;
int len1=str1.length();
int len2=str2.length();
//前面補0,弄成長度相同
if(len1<len2)
{
for(int i=1; i<=len2-len1; i++)
str1="0"+str1;
}
else
{
for(int i=1; i<=len1-len2; i++)
str2="0"+str2;
}
len1=str1.length();
int cf=0;
int temp;
for(int i=len1-1; i>=0; i--)
{
temp=str1[i]-'0'+str2[i]-'0'+cf;
cf=temp/10;
temp%=10;
str=char(temp+'0')+str;
}
if(cf!=0) str=char(cf+'0')+str;
return str;
}
//高精度減法
//只能是兩個正數相減,而且要大減小
string sub(string str1,string str2)//高精度減法
{
string str;
int tmp=str1.length()-str2.length();
int cf=0;
for(int i=str2.length()-1; i>=0; i--)
{
if(str1[tmp+i]<str2[i]+cf)
{
str=char(str1[tmp+i]-str2[i]-cf+'0'+10)+str;
cf=1;
}
else
{
str=char(str1[tmp+i]-str2[i]-cf+'0')+str;
cf=0;
}
}
for(int i=tmp-1; i>=0; i--)
{
if(str1[i]-cf>='0')
{
str=char(str1[i]-cf)+str;
cf=0;
}
else
{
str=char(str1[i]-cf+10)+str;
cf=1;
}
}
str.erase(0,str.find_first_not_of('0'));//去除結果中多餘的前導0
return str;
}
//高精度乘法
//只能是兩個正數相乘
string mul(string str1,string str2)
{
string str;
int len1=str1.length();
int len2=str2.length();
string tempstr;
for(int i=len2-1; i>=0; i--)
{
tempstr="";
int temp=str2[i]-'0';
int t=0;
int cf=0;
if(temp!=0)
{
for(int j=1; j<=len2-1-i; j++)
tempstr+="0";
for(int j=len1-1; j>=0; j--)
{
t=(temp*(str1[j]-'0')+cf)%10;
cf=(temp*(str1[j]-'0')+cf)/10;
tempstr=char(t+'0')+tempstr;
}
if(cf!=0) tempstr=char(cf+'0')+tempstr;
}
str=add(str,tempstr);
}
str.erase(0,str.find_first_not_of('0'));
return str;
}
//高精度除法
//兩個正數相除,商為quotient,餘數為residue
//需要高精度減法和乘法
void div(string str1,string str2,string "ient,string &residue)
{
quotient=residue="";//清空
if(str2=="0")//判斷除數是否為0
{
quotient=residue="ERROR";
return;
}
if(str1=="0")//判斷被除數是否為0
{
quotient=residue="0";
return;
}
int res=compare(str1,str2);
if(res<0)
{
quotient="0";
residue=str1;
return;
}
else if(res==0)
{
quotient="1";
residue="0";
return;
}
else
{
int len1=str1.length();
int len2=str2.length();
string tempstr;
tempstr.append(str1,0,len2-1);
for(int i=len2-1; i<len1; i++)
{
tempstr=tempstr+str1[i];
tempstr.erase(0,tempstr.find_first_not_of('0'));
if(tempstr.empty())
tempstr="0";
for(char ch='9'; ch>='0'; ch--) //試商
{
string str,tmp;
str=str+ch;
tmp=mul(str2,str);
if(compare(tmp,tempstr)<=0)//試商成功
{
quotient=quotient+ch;
tempstr=sub(tempstr,tmp);
break;
}
}
}
residue=tempstr;
}
quotient.erase(0,quotient.find_first_not_of('0'));
if(quotient.empty()) quotient="0";
}
int Equal(string str1,string str2)//判斷兩個數是否相等
{
int i,flag=1;
for(i=0; str1[i]!='\0'&&str2[i]!='\0'; ++i)
if(str1[i]!=str2[i])
{
flag=0;
break;
}
return flag;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin>>t;
while(t--)
{
string n,m;
string x1,y1,x2,y2;
string q="11";
string p="0";
cin>>n>>m;
//cout<<add(str1,str2)<<endl;
//cout<<sub(str1,str2)<<endl;
//cout<<mul(str1,str2)<<endl;
if(!Equal(n,m))//男女生人數要相同
{
cout<<"NO"<<endl;
continue;
}
div(n,q,x1,y1);
if(!Equal(y1,p))//男女人數要是11的倍數
{
cout<<"NO"<<endl;
continue;
}
div(m,q,x2,y2);
if(!Equal(y2,p))
{
cout<<"NO"<<endl;
continue;
}
if(!Equal(x1,x2))//不能有被剩下的人
{
cout<<"NO"<<endl;
continue;
}
cout<<"YES"<<endl;
}
return 0;
}
相關文章
- 第十屆山東省大學生程式設計競賽題解(A、F、M、C)程式設計
- 第15屆浙江省大學生程式設計競賽D題程式設計
- 第二十屆西南科技大學ACM程式設計競賽(同步賽)ACM程式設計
- 無錫學院2024年ACM大學生程式設計競賽校選賽 題解ACM程式設計
- [題解][2021-2022年度國際大學生程式設計競賽第10屆陝西省程式設計競賽] Type The Strings程式設計
- 紹興市大學生程式設計競賽程式設計
- 2019山東ACM省賽補題題解ACM
- 2020 年第一屆遼寧省大學生程式設計競賽 D.開心消消樂(點分治)程式設計
- 第 10 屆 CCPC 中國大學生程式設計競賽濟南站 遊記程式設計
- 華中農業大學第十三屆程式設計競賽程式設計
- [補題] 第 45 屆國際大學生程式設計競賽(ICPC)亞洲區域賽(上海)程式設計
- 中國大學生數學競賽(非數學專業類)競賽大綱
- 第十屆中國大學生程式設計競賽 重慶站(CCPC 2024 Chongqing Site)程式設計
- 2023 國際大學生程式設計競賽亞洲區域賽(濟南站)(SMU Autumn 2024 Team Round 2)程式設計
- 第43屆ACM-ICPC國際大學生程式設計競賽 亞洲區域賽南京站現場賽名額分配相關說明ACM程式設計
- 華中農業大學第十三屆程式設計競賽 題解程式設計
- 九州信泰杯 第十一屆山東省網路安全技能大賽
- 北京資訊科技大學第十一屆程式設計競賽(重現賽)I程式設計
- 湖南大學2020屆ACM新生賽 部分題解ACM
- 24山東省賽wp
- 競真我 贏未來:首屆大學生數字體育競技大賽釋出會召開
- 大學生電子設計競賽電源資料
- 第十五屆浙江大學寧波理工學院程式設計大賽(同步賽)程式設計
- 2019年第二屆全國大學生大資料技能競賽通知大資料
- 2024 CCPC第五屆遼寧省程式設計競賽 集訓2程式設計
- 2024廣東大學生攻防大賽WP
- 2022廣東大學生攻防大賽WP
- 2020“數維杯”國際大學生數學建模競賽賽題分析
- 【比賽覆盤】2024第七屆“傳智杯”全國大學生計算機大賽程式設計挑戰賽(初賽第一場)計算機程式設計
- “位元組跳動杯”2018中國大學生程式設計競賽-女生專場程式設計
- 2020年“感恩杯”台州學院第十三屆大學生程式設計競賽D、H、I題解(後續補充)程式設計
- 第二屆“祥雲杯”網路安全大賽暨吉林省第四屆大學生網路安全大賽火熱報名中
- 第二屆“重科杯”重慶科技大學程式設計競賽(同步賽)ptlks的題解(2024.5.18)程式設計
- M-災難預警-浙江農林大學第十九屆程式設計競賽暨天梯賽選拔賽程式設計
- 第十四屆全國大學生資訊保安競賽——創新實踐能力賽(東北賽區)比賽圓滿落幕
- 第十五屆藍橋杯大賽軟體賽省賽 C/C++ 大學 A 組C++
- 程式設計天才“樓教主”—— 專訪兩屆“黑客杯”世界程式設計大賽季軍、清華大學博士生樓天城...程式設計黑客
- 2018全國大學生數學建模競賽論文釋出
- 第九屆中國大學生程式設計競賽 深圳站(CCPC 2023 Shenzhen Site)/ The 2nd Universal Cup. Stage 25: Shenzhen程式設計