DescriptionDescribe
Find amount of numbers for given sequence of integers numbers such that after raising them to the M-th Power they Would be divided by K.
The number of a sequence of n integers, in which the m of a number can be divisible by K.
InputInput
Input consists of lines. There is three integer numbers N, M, K (0<n, M, k<10001) on the first line. There is N positive an integer numbers−given sequence (each number isn't more than 10001) −on the second line.
The input contains two lines. The first line consists of three integers N, M, K (0 < N, M, K < 10001).
The second line contains n positive integers-a given sequence (no more than 10001 per number).
OutputOutput
Write answer for given task.
Outputs the answer for a given task.
Sample InputSample input
4 2 50
9 10 11 12
Sample OutputSample output
1
AnalysisAnalysis
Fast power, Time complexity O (n logn), should be able to.
Be aware that using int will overflow, so I used the unsigned long long directly.
This topic also has a method is the qualitative factor decomposition, to find out the number of factors after the M-time (that is, the number of elements multiplied by m), and then the number of M-factor comparison can be.
SolutionSolution Solutions
Fast power:
#include <iostream>using namespace Std;typedef unsigned long long ull;ull Pow (ull x, ull y, ull z); int main () {UL L ntmp;int N, M, K;while (CIN >> n >> M >> K) {int ncnt = 0;for (int i = 1; I <= N; i++) {cin >> NT Mp;if (Pow (ntmp, M, K) = = 0) {ncnt++;}} cout << ncnt << Endl;} return 0;} Ull Pow (ull x, ull y, ull z) {if (y = = 1) {return x% Z;} ull ntmp = Pow (x, Y/2, z); if (Y & 1) {return (ull) ntmp * ntmp * x% Z;} else {return (ull) ntmp * ntmp% Z;}}
Mass factor decomposition:
#include <iostream> #include <memory.h>using namespace std;const int MAX = 10240;int X[max], y[max];void Fact ( int x, int *p); int main () {int ntmp;int n, M, K;while (CIN >> n >> M >> K) {int ncnt = 0;memset (Y, 0, Sizeo F (Y)); Fact (K, Y); for (int i = 1; I <= N; i++) {memset (x, 0, sizeof (x)); Cin >> ntmp; Fact (ntmp, X); for (int i = 0; i < MAX; i++) {x[i] *= M;} BOOL bflag = true;for (int j = 0; J < MAX; J + +) {if (X[j] < y[j]) {bflag = false; break;}} if (bflag) {ncnt++;}} cout << ncnt << Endl;} return 0;} void Fact (int x, int *p) {for (int i = 2; i <= x; i++) {if (x% i = = 0) {while (x% i = = 0) {(* (P + i)) ++;x/= I;}}}
This topic uses the fast power needed to convert the divisible into mod after 0.
SGU[117] Counting