SGU[117] Counting

Description

描述

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.

求出给定一个包含N个整数的序列,其中某个数字的M次方能被K整除的数的个数。

Input

输入

Input consists of two lines. There are three integer numbers N, M, K (0<N, M, K<10001) on the first line. There are N positive integer numbers − given sequence (each number is not more than 10001) − on the second line.

输入包含两行。第一行包含三个整数N, M, K (0 < N, M, K < 10001)。

第二行包含N个正整数——给定的序列(每个数不超过10001)。

Output

输出

Write answer for given task.

输出给定任务的答案。

Sample Input

样例输入

4 2 50

9 10 11 12

Sample Output

样例输出

1

Analysis

分析

快速幂,时间复杂度为O(n logn),应该是可以过的。

要注意用int的话会溢出,所以我直接用了unsigned long long。

这道题目还有一个方法是质因数分解,求出M次方以后的各个因数个数(就是把个因子个数乘以M),然后和M的个因子的个数比较即可。

Solution

解决方案

快速幂:

#include <iostream>

using namespace std;

typedef unsigned long long  ull;

ull Pow(ull x, ull y, ull z);

int main()
{
	ull nTmp;
	int N, M, K;
	while(cin >> N >> M >> K)
	{
		int nCnt = 0;
		for(int i = 1; i <= N; i++)
		{
			cin >> nTmp;
			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; }
}

质因数分解:

#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, sizeof(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;
			}
		}
	}
}

  

这道题目使用快速幂需要将整除转换成mod以后余0。

时间: 2024-10-06 21:02:48

SGU[117] Counting的相关文章

SGU - 117 - Counting (快速幂取模!)

SGU - 117 Counting Time Limit: 250MS   Memory Limit: 4096KB   64bit IO Format: %I64d & %I64u Submit Status Description 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

Counting - SGU 117(快速幂)

题目大意:求下面N个数里面有多少个数的M次方能整除K 代码如下: ======================================================== #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int MAXN = 10007; const int oo = 1e9+7; int QuickPow(int a, int

SGU 438 The Glorious Karlutka River =) 拆点+动态流+最大流

The Glorious Karlutka River =) Time Limit:500MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice SGU 438 Appoint description: Description A group of Mtourists are walking along the Karlutka river. They want to cross

HDU 5862 Counting Intersections

题目:Counting Intersections 链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 题意:给出n 条平行于坐标轴的线段,问这些线段有多少交点(题目保证没有两条线段共享一个端点.保证没有重叠.保证线段长度大于0)n范围10万 思路: 将n 条线段2n 个点按x 排序,然后当遇到一个横向的左端点时,对应的y++,遇到右端点,y--,遇到竖线,交点数目加上 下端点到上端点 之间的y 的和.离散化加树状数组可做.要注意细节,

sgu Kalevich Strikes Back

这道题就是求一个大矩形被n个矩形划分成n+1个部分的面积,这些矩形之间不会相交,可能包含.. 1 #include <cstdio> 2 #include <cstring> 3 #include <vector> 4 #include <algorithm> 5 #define maxn 120100 6 using namespace std; 7 8 long long s[maxn]; 9 vector<int>g[maxn]; 10 i

find out the neighbouring max D_value by counting sort in stack

1 #include <stdio.h> 2 #include <malloc.h> 3 #define MAX_STACK 10 4 5 int COUNT = 0; 6 // define the node of stack 7 typedef struct node { 8 int data; 9 node *next; 10 }*Snode; 11 12 // define the stack 13 typedef struct Stack{ 14 Snode top; 1

数学计数原理(P&#243;lya,高精度):SGU 294 He&#39;s Circles

He's Circles He wrote n letters "X" and "E" in a circle. He thought that there were 2n possibilities to do it, because each letter may be either "X" or "E". But Qc noticed that some different sequences of letters ca

sgu Ice-cream Tycoon

题意:供应商提供n块价格为c的冰淇淋,一个学生想买n块冰淇淋,手中的钱数总共有t元,为了不让买n块冰淇淋所花费的钱数不超过t元,先尽可能卖给这个学生便宜的冰淇淋. 如果这个学生不能买到所需要的冰淇淋则输出“UNHAPPY”,能则输出“HAPPY”. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define maxn 200000 5 using namespace std; 6 7

sdut 2610:Boring Counting(第四届山东省省赛原题,划分树 + 二分)

Boring Counting Time Limit: 3000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 In this problem you are given a number sequence P consisting of N integer and Pi is the ith element in the sequence. Now you task is to answer a list of queries, for each quer