问题链接
判断n是否是质数
#include<iostream>
#include<cstdio>
using namespace std;
int isPrime(int x) {
if (x < 2)
return 0;
else if (x == 2)
return 1;
if (x % 2 == 0)
return 0;
for (int i = 3; i * i <= x; i += 2)
if (x % i == 0)
return 0;
return 1;
}
int main()
{
int n, t, cnt = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &t);
if (isPrime(t))
cnt++;
}
printf("%d\n", cnt);
return 0;
}
题目链接
ALDS1_1_B:Greatest Common Divisor
求两个数的最大公约数
#include<iostream>
#include<cstdio>
using namespace std;
int gcd(int x, int y) {
return y ? gcd(y, x % y): x;
}
int main()
{
int x, y;
scanf("%d %d", &x, &y);
printf("%d\n", gcd(x, y));
return 0;
}
问题链接
求 mn 的值求模1000000007
#include<iostream>
#include<cstdio>
using namespace std;
typedef long long LL;
LL mod_pow(LL n, LL m, LL mod) {
LL res = 1;
while (m > 0) {
if (m & 1)
res = res * n % mod;
n = n * n % mod;
m >>= 1;
}
return res;
}
int main()
{
LL n, m;
LL mod = 1000000007;
scanf("%lld %lld", &n, &m);
printf("%d\n", mod_pow(n, m , mod));
return 0;
}