首页 > 其他 > 详细

SGU117 水题 Easy

时间:2014-01-31 14:53:19      阅读:414      评论:0      收藏:0      [点我收藏+]

问题: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;
}


SGU117 水题 Easy

原文:http://blog.csdn.net/lotus_land/article/details/18883063

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!