cppAcademy
FROM LEARNING TO COMPETING

Intermediate.
Put your skills to the test.

Bulgarian Informatics Group D · 2007–2026

270 independently explained problems: 54 Municipal, 54 Regional, 105 National Olympiad and 57 Autumn Tournament tasks. Start with the easier tasks and work upwards. Difficulty labels are learning estimates, not official ratings; Group D also includes challenging national-level problems. The six 2017 National tasks shared by Groups C and D use their existing permanent URLs. Years follow Arena’s competition-season labels. Source clarifications and special grader requirements are noted on affected tasks.
GROUP D2010 · EasyDivisor enumeration

Numbers with a Given Proper-Divisor Count

National Olympiad — Municipal round (2010 season)

Submission name: deliteli.cpp

Problem summary

Count sequence entries having exactly m positive divisors other than 1 and the number itself.

Source clarification. The printed entry bound is 1000, while its own example contains 1024. This divisor-pair implementation handles both without changing the definition.

Input

n and m, followed by n sequence entries.

Output

The number of qualifying entries.

Constraints

1 ≤ n ≤ 200; 1 ≤ m ≤ 10. The printed value bound is 1000, but the official sample includes 1024.

Worked example

Input and output for the worked example discussed below.

Input

7 4
6 20 12 64 1024 50 24

Output

3

Solution analysis

  1. Positive divisors appear in pairs d and value/d. Test possible smaller divisors starting at 2, stopping at d²>value. Starting at 2 excludes the forbidden pair 1,value.
  2. When d divides value, add two for its pair unless d²=value, in which case the square-root divisor occurs only once. Compare this proper-divisor count with m and count each qualifying sequence occurrence.
  3. For 20, the proper divisors are 2,4,5,10; for 12 they are 2,3,4,6; for 50 they are 2,5,10,25. Thus exactly these three sample entries have four proper divisors. Equal entries, if present, are counted separately.

Why it works

Every proper divisor belongs to one pair with a smaller member at most sqrt(value). The loop visits exactly that smaller member; square roots are counted once and other pairs twice. Consequently the computed count includes every allowed divisor exactly once.

Complexity: O(n√V) time and O(1) auxiliary space, for maximum positive entry V. A streaming scan avoids storing the sequence.

Watch out. Do not count 1 or the number itself, or double-count a perfect-square root. Treat 1 as having zero proper divisors. The source intends positive numbers for its divisor definition.

Commented C++17 solution

An independently written complete program with standard headers.

#include<algorithm>
#include<array>
#include<cmath>
#include<iostream>
#include<limits>
#include<map>
#include<numeric>
#include<set>
#include<string>
#include<vector>
using namespace std;
int main(){
    int count;
    int wanted;
    cin>>count>>wanted;
    int answer=0;
    for(int index=0;index<count;index++){
        int value;
        cin>>value;
        int divisors=0;
        for(int d=2;d*d<=value;d++){
            if(value%d==0){
                // Equal factor pairs contribute just their square-root member.
                divisors+=d*d==value?1:2;
            }
        }
        if(divisors==wanted){
            answer++;
        }
    }
    cout<<answer<<'\n';
}

Source: Arena Olimpiici ↗