当前位置:网站首页>[exercise 4-1] cake distribution
[exercise 4-1] cake distribution
2022-07-06 15:57:00 【Flame car】
List of articles
A . Cake Distribution
Description
You are about to have a birthday and you would like to prepare a birthday cake that weighs anywhere between 1 and 1018 grams inclusive. You know that there will be A,B or C guests at the party. You would like to cut this cake into pieces such that:
The weight of each piece is a positive integer number of grams.
No matter how many guests arrive, the pieces can be distributed between the guests in such a way that each guest gets the same amount of cake. Note that some guests might get more than one piece.
You don’t want to spend too much time cutting the cake, so you would like to have at most 5,000 pieces. Let’s do it!
Input
The only line of input contains the 3 numbers A,B,C, all positive integers not more than 1000.
Output
On the first line, output one number K, the number of pieces. On each of the next K lines, output a description of each piece, consisting of 4 numbers, wi,ai,bi,ci, where wi is the weight of the piece in grams and ai,bi,ci are indices of the person who will get this piece if A,B, or C guests arrive, respectively. The indices should satisfy 1≤ai≤A, 1≤bi≤B, 1≤ci≤C. The sum of all wis must be less than or equal to 1018.
Samples
Input
1 2 3
Output
4
2 1 1 1
1 1 1 2
1 1 2 2
2 1 2 3
1. Source of topic
The question is Waterloo Local Contest 2019 Fall Inside A topic .
Official title link : Click on
Official solutions and data : Click on
2. Topic translation
At first, I didn't understand what I said , Later on N Many translators have learned such a situation :
You have a biggest 1e18 The cake , There may be A Or B Or C personal ( One of them ) Come and eat cake , When distributing cakes , Everyone must be assigned to the cake Same quality ( The number can vary , But the quality must be the same ), Because you are lazy , At most 5000 block ( This determines that you cannot run with violence ).
Then explain the example :
First output how many pieces you divided N( It's up to you , There is a special judgment , No more than 5000 block )
And then down N That's ok , Each row
The first one is to output the quality of this cake .
The second output, if any A How many people will get this cake .
Third output, if any B How many people will get this cake .
The fourth output, if any C How many people will get this cake .
Code
Error model ( Ignore most 5000 block )
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
const int N = 1e6+5;
int main()
{
ll a,b,c;
cin>>a>>b>>c;
ll t = a*b*c;
cout<<t<<endl;
ll na=1,nb=1,nc=1;
ll cnta=0,cntb=0,cntc=0;
ll moda,modb,modc;
moda = b*c;
modb = a*c;
modc = a*b;
for(ll i=1;i<=t;i++) {
printf("1 %lld %lld %lld\n",na,nb,nc);
if(++cnta%moda==0)
na++;
if(++cntb%modb==0)
nb++;
if(++cntc%modc==0)
nc++;
}
return 0;
}
This is the assumption that every piece is 1g Calculated , It also depends on the time limit 15s Dare to write like this .
Correct demonstration
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
const int N = 1e6+5;
int cnt;
map<ll,int> mp;
ll num[N];
int main()
{
ll a,b,c;
cin>>a>>b>>c;
for(int i = 1;i <= a; i++)
if(!mp[b*c*i])
num[++cnt]=b*c*i,mp[b*c*i]=1;
for(int i = 1;i <= b; i++)
if(!mp[a*c*i])
num[++cnt]=a*c*i,mp[a*c*i]=1;
for(int i = 1;i <= c; i++)
if(!mp[b*a*i])
num[++cnt]=b*a*i,mp[b*a*i]=1;
sort(num+1,num+1+cnt);
ll last = 0;
cout << cnt << endl;
for (int i=1;i<=cnt;i++) {
cout<<num[i]-last<<' '<<(last/(b*c) + 1)<<' '<<(last/(a*c)+1)<<' '<<(last/(b*a)+1)<<'\n';
last = num[i];
}
return 0;
}
Simple version
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
const int N = 1e6+5;
set<ll> st;
int main()
{
ll a,b,c;
cin>>a>>b>>c;
for(int i = 1;i <= a; i++)
st.insert(b*c*i);
for(int i = 1;i <= b; i++)
st.insert(a*c*i);
for(int i = 1;i <= c; i++)
st.insert(b*a*i);
ll last = 0;
cout << st.size() << endl;
for (auto x : st) {
cout<<x-last<<' '<<(last/(b*c) + 1)<<' '<<(last/(a*c)+1)<<' '<<(last/(b*a)+1)<<'\n';
last = x;
}
return 0;
}
The concise version actually uses set Instead of using map Duplicate check , as well as sort Sorting steps , because set It will automatically check the duplicate and sort .
auto x : st
set<int>::iterator it = st.begin();it!=st.end();it++
Both are acceptable , Obviously, the former is simpler .auto Is to get the type automatically .
The former uses x Substitute value , The latter uses *it.
analysis
At first glance, it may feel messy , In fact, if you look carefully, you can understand the truth :
This is actually a super optimization of violence , The time complexity is directly from O(1e9) drop to O(1e3). In terms of ideology, it is exactly the same . Traversal just merges some that can be merged , In fact, the total quality M = a×b×c There is no change
① The first three times for What does cycle mean ?
Take one for example ( With A For example ), Every time b×c Gram belongs to one person ( The total quality a×b×c, common a Share , each b×c)
② Why use This time x- Last time x?
Or the reason above , The quality of everyone is a( or b or c), We pause once ( Do one operation ) There is At least one group needs to be replaced . Due to the accumulation of a( or b or c) Gram's cake was given to one person , You need to change someone , So this operation is to output how many cakes are separated in this small step .(x Is the cumulative value )
③last/(b×c) + 1) What does that mean? ?
Replacement operation , When last It's you insert At the time of the i×b×c when , Now the one who is sharing the cake is last/(b×c) + 1 Number
Give me a demonstration :
1 2 3
↓
1 --> 6
2 --> 3 6
3 --> 2 4 6
That is, accumulated to 6g when A substitutions
Accumulate to 3g ,6g when B substitutions
Accumulate to 2g , 4g, 6g when C substitutions
After checking and sorting --> 2 3 4 6
That is, it is accumulating to 2,3,4,6g When you need to change people .
The first size 2-0=2 ,1 1 1 , here C substitutions .
The second size 3-2=1 ,1 1 2 , here B substitutions .
The third size 4-3=1 ,1 2 2 , here C substitutions .
The fourth size 6-4=2, 1 2 3 , here A,B,C Change people .
So in fact, this is the optimization of violence .
Violence is :
1 1 1 1
1 1 1 1
1 1 1 2
1 1 2 2
1 1 2 3
1 1 2 3
A Every time 6 Change one at a time ,B Every time 3 Change one at a time ,C Every time 2 Change one at a time .( The focus is the replacement of a combination )
Optimization is :
2 1 1 1
1 1 1 2
1 1 2 2
2 1 2 3
The optimized version is to put the repeated quality together ( The focus is on the replacement of each combination )
边栏推荐
猜你喜欢

D - Function(HDU - 6546)女生赛
![[analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class](/img/3b/dc43564a36f82e73826b08f39c935e.png)
[analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class

C语言必背代码大全

Learning record: use STM32 external input interrupt

信息安全-安全编排自动化与响应 (SOAR) 技术解析

渗透测试 ( 5 ) --- 扫描之王 nmap、渗透测试工具实战技巧合集

Learning record: USART serial communication

Optimization method of path problem before dynamic planning

Information security - threat detection - detailed design of NAT log access threat detection platform

Learning records: serial communication and solutions to errors encountered
随机推荐
China's earthwork equipment market trend report, technical dynamic innovation and market forecast
STM32 how to use stlink download program: light LED running light (Library version)
C语言是低级和高级的分水岭
F - Birthday Cake(山东省赛)
Accounting regulations and professional ethics [3]
0 - 1 problème de sac à dos (1)
【练习-11】4 Values whose Sum is 0(和为0的4个值)
mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’
差分(一维,二维,三维) 蓝桥杯三体攻击
渗透测试 ( 3 ) --- Metasploit Framework ( MSF )
【练习-2】(Uva 712) S-Trees (S树)
Path problem before dynamic planning
【练习-6】(Uva 725)Division(除法)== 暴力
Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools
HDU-6025-Coprime Sequence(女生赛)
[exercise-3] (UVA 442) matrix chain multiplication
对iptables进行常规操作
Learning record: use STM32 external input interrupt
Essai de pénétration (1) - - outils nécessaires, navigation
Information security - security professional name | CVE | rce | POC | Vul | 0day