问题:N个数中有多少个数的M次方能被K整除。
Problem: Find amount of numbers for given sequence of integer numbers such that after raising them to the M-th power they will be divided by K.
解法:注意到每个数不超10000,这个题就沦为水题了。求质数,分解质因数,再依次比较质因数的指数。
Solution: This problem become very easy if you note that each number is not larger than 10000. Calculate the prime numbers, then decompose the numbers into prime factors. Finally, compare the exponents of each prime factors of K and the number.
#include <iostream> #include <stdio.h> #include <cstring> using namespace std; int p[10010],q[10010],r[10010]; int n,m,k,pnum,x; bool f[10010]; int main() { pnum = 0; memset(f,0,sizeof(f)); for (int i=2;i<=10005;i++) if (!f[i]) { p[++pnum] = i; for (int j=i;j<=10005;j+=i) f[j] = true; } while (~scanf("%d%d%d",&n,&m,&k)) { memset(q,0,sizeof(q)); for (int i=1;i<=pnum;i++) while (k%p[i]==0) { k /= p[i]; q[i]++; } int sum = 0; for (int i=1;i<=n;i++) { memset(r,0,sizeof(r)); scanf("%d",&x); for (int j=1;j<=pnum;j++) while (x%p[j]==0) { x /= p[j]; r[j]++; } bool find = true; for (int j=1;j<=pnum;j++) { r[j] *= m; if (r[j]<q[j]) { find = false; break; } } if (find) sum++; } printf("%d\n",sum); } return 0; }