【SGU】117. Counting 快速幂

传送门:【SGU】117. Counting


题目分析:快速幂对k取模,结果为0则ans++。


代码如下:


#include <map>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std ;

typedef long long LL ;

#pragma comment ( linker , "/STACK:16777216" )
#define rep( i , a , b ) for ( int i = ( a ) ; i <  ( b ) ; ++ i )
#define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )
#define clr( a , x ) memset ( a , x , sizeof a )

int n , m , k ;

int pow ( int a , int b ) {
	int res = 1 , tmp = a ;
	while ( b ) {
		if ( b & 1 ) res = res * tmp % k ;
		tmp = tmp * tmp % k ;
		b >>= 1 ;
	}
	return res ;
}

void solve () {
	int x , ans = 0 ;
	For ( i , 1 , n ) {
		scanf ( "%d" , &x ) ;
		if ( !pow ( x , m ) ) ++ ans ;
	}
	printf ( "%d\n" , ans ) ;
}

int main () {
	while ( ~scanf ( "%d%d%d" , &n , &m , &k ) ) solve () ;
	return 0 ;
}


你可能感兴趣的:(sgu)