Two positive integers N and M are given. Integer N represents the number of chocolates arranged in a circle, numbered from 0 to N − 1.
You start to eat the chocolates. After eating a chocolate you leave only a wrapper.
You begin with eating chocolate number 0. Then you omit the next M − 1 chocolates or wrappers on the circle, and eat the following one.
More precisely, if you ate chocolate number X, then you will next eat the chocolate with number (X + M) modulo N (remainder of division).
You stop eating when you encounter an empty wrapper.
For example, given integers N = 10 and M = 4. You will eat the following chocolates: 0, 4, 8, 2, 6.
The goal is to count the number of chocolates that you will eat, following the above rules.
Write a function:
int solution(int N, int M);
that, given two positive integers N and M, returns the number of chocolates that you will eat.
For example, given integers N = 10 and M = 4. the function should return 5, as explained above.
Write an efficient algorithm for the following assumptions:
- N and M are integers within the range [1..1,000,000,000].
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(int N, int M) {
// write your code in C++14 (g++ 6.2.0)
vector<int> eat_flag(N, 0);
for(int i=0; i<N; i++){
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(int N, int M) {
// write your code in C++14 (g++ 6.2.0)
vector<int> eat_flag(N, 0);
for(int i=0; i<N; i++){
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(int N, int M) {
// write your code in C++14 (g++ 6.2.0)
vector<int> eat_flag(N, 0);
for(int i=0; i<N; i++){
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(int N, int M) {
// write your code in C++14 (g++ 6.2.0)
vector<int> eat_flag(N, 0);
for(int i=0; i<N; i++){
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(int N, int M) {
// write your code in C++14 (g++ 6.2.0)
vector<int> eat_flag(N, 0);
for(int i=0; i<N; i++){
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(int N, int M) {
// write your code in C++14 (g++ 6.2.0)
vector<int> eat_flag(N, 0);
for(int i=0; i<N; i++){
int idx = i*
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(int N, int M) {
// write your code in C++14 (g++ 6.2.0)
vector<int> eat_flag(N, 0);
for(int i=0; i<N; i++){
int idx = i*M
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(int N, int M) {
// write your code in C++14 (g++ 6.2.0)
vector<int> eat_flag(N, 0);
for(int i=0; i<N; i++){
int idx = i(*M
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(int N, int M) {
// write your code in C++14 (g++ 6.2.0)
vector<int> eat_flag(N, 0);
for(int i=0; i<N; i++){
int idx = (i*M)%M;
eat_flag[idx] = false;
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(int N, int M) {
// write your code in C++14 (g++ 6.2.0)
vector<int> eat_flag(N, 0);
for(int i=0; i<N; i++){
int idx = (i*M)%M;
eat_flag[idx] = 1;
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(int N, int M) {
// write your code in C++14 (g++ 6.2.0)
vector<int> eat_flag(N, 0);
for(int i=0; i<N; i++){
int idx = (i*M)%M;
if()
eat_flag[idx] = 1;
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(int N, int M) {
// write your code in C++14 (g++ 6.2.0)
vector<int> eat_flag(N, 0);
for(int i=0; i<N; i++){
int idx = (i*M)%M;
if(eat_flag[idx] == 0){
eat_flag[idx] = 1;
}eat_flag[idx] = 1;
eat_flag[idx] = 1;
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(int N, int M) {
// write your code in C++14 (g++ 6.2.0)
vector<int> eat_flag(N, 0);
int count = 0;
for(int i=0; i<N; i++){
int idx = (i*M)%M;
if(eat_flag[idx] == 0){
eat_flag[idx] = 1;
count++;
}eat_flag[idx] = 1;
eat_flag[idx] = 1;
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(int N, int M) {
// write your code in C++14 (g++ 6.2.0)
vector<int> eat_flag(N, 0);
int count = 0;
for(int i=0; i<N; i++){
int idx = (i*M)%M;
if(eat_flag[idx] == 0){
eat_flag[idx] = 1;
count++;
}else{
eat_flag[idx] = 1;
}
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(int N, int M) {
// write your code in C++14 (g++ 6.2.0)
vector<int> eat_flag(N, 0);
int count = 0;
for(int i=0; i<N; i++){
int idx = (i*M)%M;
if(eat_flag[idx] == 0){
eat_flag[idx] = 1;
count++;
}else{
break;
}
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(int N, int M) {
// write your code in C++14 (g++ 6.2.0)
vector<int> eat_flag(N, 0);
int count = 0;
for(int i=0; i<N; i++){
int idx = (i*M)%M;
if(eat_flag[idx] == 0){
eat_flag[idx] = 1;
count++;
}else{
break;
}
}
return count;
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(int N, int M) {
// write your code in C++14 (g++ 6.2.0)
}
int calc_gcd(int a, int b){
if(a == b){
return
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(int N, int M) {
// write your code in C++14 (g++ 6.2.0)
}
int calc_gcd(int a, int b){
if(b == 0){
return a;
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(int N, int M) {
// write your code in C++14 (g++ 6.2.0)
}
int calc_gcd(int a, int b){
if(b == 0){
return a;
}else{
return calc
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(int N, int M) {
// write your code in C++14 (g++ 6.2.0)
}
int calc_gcd(int a, int b){
if(b == 0){
return a;
}else{
return calc_gcd(b, a%b)
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(int N, int M) {
// write your code in C++14 (g++ 6.2.0)
}
int calc_gcd(int a, int b){
if(b == 0){
return a;
}else{
return calc_gcd(b, a%b);
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int calc_gcd(int a, int b){
if(b == 0){
return a;
}else{
return calc_gcd(b, a%b);
}
}
int solution(int N, int M) {
// write your code in C++14 (g++ 6.2.0)
int gcd = calc_gcd(N, M);
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int calc_gcd(int a, int b){
if(b == 0){
return a;
}else{
return calc_gcd(b, a%b);
}
}
int solution(int N, int M) {
// write your code in C++14 (g++ 6.2.0)
int gcd = calc_gcd(N, M);
int res = N/gcd;
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int calc_gcd(int a, int b){
if(b == 0){
return a;
}else{
return calc_gcd(b, a%b);
}
}
int solution(int N, int M) {
// write your code in C++14 (g++ 6.2.0)
int gcd = calc_gcd(N, M);
int res = N/gcd;
return res;
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int calc_gcd(int a, int b){
if(b == 0){
return a;
}else{
return calc_gcd(b, a%b);
}
}
int solution(int N, int M) {
// write your code in C++14 (g++ 6.2.0)
int gcd = calc_gcd(N, M);
int res = N/gcd;
return res;
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int calc_gcd(int a, int b){
if(b == 0){
return a;
}else{
return calc_gcd(b, a%b);
}
}
int solution(int N, int M) {
// write your code in C++14 (g++ 6.2.0)
int gcd = calc_gcd(N, M);
int res = N/gcd;
return res;
}
The solution obtained perfect score.