import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
if (isPrime(x)) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
sc.close();
}
public static boolean isPrime(int x) {
if (x < 2) {
return false;
}
int sqrt = (int)Math.pow(x, 0.5);
for (int i = 2; i <= sqrt; i++) {
if (x % i == 0) {
return false;
}
}
return true;
}
}
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
getFactor(x);
System.out.println();
}
sc.close();
}
public static void getFactor(int x) {
int sqrt = (int)Math.pow(x, 0.5);
for (int i = 2; x > 1 && i <= sqrt; i++) {
int cnt = 0;
while (x % i == 0) {
x /= i;
cnt++;
}
if (cnt > 0) {
System.out.println(i + " " + cnt);
}
}
if (x > 1) {
//输出大于sqrt的因子
System.out.println(x + " " + 1);
}
}
}
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int res = 0;
//false是质数,true不是质数
boolean[] flag = new boolean[n + 1];
for (int i = 2; i <= n; i++) {
if (!flag[i]) {
res++;
for (int j = 2; j * i <= n; j++) {
flag[j * i] = true;
}
}
}
System.out.println(res);
sc.close();
}
}
线性筛法(欧拉筛法):
思想仍然是排除法
核心:每个合数一定会被其最小质因子筛掉,且只筛一次
记录下当前的质数,从小到大遍历,排除后面的合数,适当的时机停下来,保证只筛一次
为什么保证一定是最小质因子?
答:分为以下两种情况:(prime[j] 简称为pj)
i % pj == 0 pj一定是i的最小质因子,pj一定是pj * i的最小质因子
i % pj != 0 pj一定小于i的所有质因子,pj也一定是pj *
i的最小质因子
时线性筛比埃氏筛快约1倍
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int res = 0;
boolean[] flag = new boolean[n + 1];
int[] prime = new int[n + 1];
int cnt = 0;
for (int i = 2; i <= n; i++) {
if (!flag[i]) {
prime[cnt++] = i;
}
//判断条件不用加j < cnt,原因如下:
//1. i若为合数,就一定会被前面存下来的i的最小质因数停下来
//2. i若为质数,因为把i添加到了prime最后,所以一定会执行break
for (int j = 0; prime[j] * i <= n; j++) {
flag[prime[j] * i] = true;
//prime[j]一定是i的最小质因子
if (i % prime[j] == 0) {
break;
}
}
}
System.out.println(cnt);
sc.close();
}
}
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
getFactor(x);
System.out.println();
}
sc.close();
}
public static void getFactor(int x) {
int sqrt = (int) Math.sqrt(x);
List<Integer> res = new ArrayList<>();
for (int i = 1; i <= sqrt; i++) {
if (x % i == 0) {
res.add(i);
if (i != x / i) {
res.add(x / i);
}
}
}
Collections.sort(res);
for (int i : res) {
System.out.print(i + " ");
}
}
}
import java.util.*;
class Main {
static Map<Integer, Integer> map;
static final int MOD = 1000000007;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
map = new HashMap<>();
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
addCnt(x);
}
//必须long
long res = 1;
for (Integer i : map.values()) {
res *= i + 1;
res %= MOD;
}
System.out.println(res);
sc.close();
}
public static void addCnt(int x) {
if (x < 2) {
return;
}
int sqrt = (int) Math.sqrt(x);
for (int i = 2; x > 1 && i <= sqrt; i++) {
while (x % i == 0) {
x /= i;
map.put(i, map.getOrDefault(i, 0) + 1);
}
}
if (x > 1) {
map.put(x, map.getOrDefault(x, 0) + 1);
}
}
}
import java.util.*;
class Main {
static Map<Integer, Integer> map;
static final int MOD = 1000000007;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
map = new HashMap<>();
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
addCnt(x);
}
long res = 1;
for (Integer k : map.keySet()) {
long v = map.get(k);
long t = 1;
for (int i = 0; i < v; i++) {
//这里是可以取模的,因为不影响结果
t = (t * k + 1) % MOD;
}
res = res * t % MOD;
}
System.out.println(res);
sc.close();
}
public static void addCnt(int x) {
if (x < 2) {
return;
}
int sqrt = (int) Math.sqrt(x);
for (int i = 2; x > 1 && i <= sqrt; i++) {
while (x % i == 0) {
x /= i;
map.put(i, map.getOrDefault(i, 0) + 1);
}
}
if (x > 1) {
map.put(x, map.getOrDefault(x, 0) + 1);
}
}
}
import java.util.*;
class Main {
public static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int a = sc.nextInt(), b = sc.nextInt();
System.out.println(gcd(a, b));
}
sc.close();
}
}
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
System.out.println(getEuler(x));
}
sc.close();
}
public static int getEuler(int x) {
int res = x;
int sqrt = (int) Math.sqrt(x);
for (int i = 2; x > 1 && i <= sqrt; i++) {
if (x % i == 0) {
//先除再乘,防止溢出
res = res / i * (i - 1);
while (x % i == 0) {
x /= i;
}
}
}
if (x > 1) {
res = res / x * (x - 1);
}
return res;
}
}
import java.util.*;
class Main {
static int P;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i = 0; i < n; i++) {
int a = sc.nextInt(), b = sc.nextInt();
P = sc.nextInt();
System.out.println(fastpow(a, b));
}
sc.close();
}
public static long fastpow(long a, long b) {
if (b == 1) {
return a;
}
long t = fastpow(a, b / 2);
long res = t * t % P;
if (b % 2 == 0) {
return res;
}
return res * a % P;
}
}
import java.util.*;
class Main {
static int P;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i = 0; i < n; i++) {
int a = sc.nextInt(), b = sc.nextInt();
P = sc.nextInt();
System.out.println(fastpow(a, b));
}
sc.close();
}
public static long fastpow(long a, long b) {
long res = 1;
while (b != 0) {
if (b % 2 == 1) {
res = res * a % P;
}
a = a * a % P;
b >>= 1;
}
return res;
}
}
import java.util.*;
class Main {
static int P;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i = 0; i < n; i++) {
int a = sc.nextInt();
P = sc.nextInt();
long res = fastpow(a, P - 2);
System.out.println(gcd(a, P) == 1 ? res : "impossible");
}
sc.close();
}
public static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
public static long fastpow(long a, long b) {
long res = 1;
while (b != 0) {
if (b % 2 == 1) {
res = res * a % P;
}
a = a * a % P;
b >>= 1;
}
return res;
}
}
import java.util.*;
class Main {
static long x;
static long y;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int a = sc.nextInt(), b = sc.nextInt();
exgcd(a, b);
System.out.println(x + " " + y);
}
sc.close();
}
public static long exgcd(long a, long b) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long res = exgcd(b, a % b);
long t = x;
x = y;
y = t - a / b * y;
return res;
}
}
import java.util.*;
class Main {
static long x;
static long y;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int a = sc.nextInt(), b = sc.nextInt(), m = sc.nextInt();
// -m也能过
long t = exgcd(a, m);
if (b % t == 0) {
System.out.println((b / t * x) % m);
} else {
System.out.println("impossible");
}
}
sc.close();
}
public static long exgcd(long a, long b) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long res = exgcd(b, a % b);
long t = x;
x = y;
y = t - a / b * y;
return res;
}
}
import java.util.*;
class Main {
static long x, y;
static long a1, m1, a2, m2;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
a1 = sc.nextLong();
m1 = sc.nextLong();
for (int i = 0; i < n - 1; i++) {
a2 = sc.nextLong();
m2 = sc.nextLong();
boolean flag = merge();
if (!flag) {
return;
}
}
//防止a1为负数
a1 = Math.abs(a1);
System.out.println((m1 % a1 + a1) % a1);
sc.close();
}
private static boolean merge() {
long gcd = exgcd(a1, -a2);
if ((m2 - m1) % gcd != 0) {
System.out.println(-1);
return false;
}
long x0 = (m2 - m1) / gcd * x;
long mod = a2 / gcd;
//把通解变成最小正整数(防止爆long)
x0 = (x0 % mod + mod) % mod;
long t = a1;
a1 = a1 / gcd * a2;
m1 = t * x0 + m1;
return true;
}
private static long exgcd(long a, long b) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long res = exgcd(b, a % b);
long t = x;
x = y;
y = t - a / b * y;
return res;
}
}
import java.util.*;
class Main {
static long[][] c;
static int N = 2000;
static long P = (long) 1e9 + 7;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
c = new long[N + 1][N + 1];
c[0][0] = 1;
for (int i = 1; i <= N; i++) {
c[i][0] = 1;
for (int j = 1; j <= i; j++) {
c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % P;
}
}
for (int i = 0; i < n; i++) {
int a = sc.nextInt(), b = sc.nextInt();
System.out.println(c[a][b]);
}
sc.close();
}
}
import java.util.*;
class Main {
static long P = (long) 1e9 + 7;
static long[] jc;
static long[] ni;
static int N = (int) 1e5 + 5;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
jc = new long[N];
ni = new long[N];
jc[0] = ni[0] = 1;
for (int i = 1; i < N; i++) {
jc[i] = (jc[i - 1] * i) % P;
//ni[i] = fastpow(jc[i], P - 2); 都能ac
ni[i] = ni[i - 1] * fastpow(i, P - 2) % P;
}
for (int i = 0; i < n; i++) {
int a = sc.nextInt(), b = sc.nextInt();
System.out.println(jc[a] * ni[a - b] % P * ni[b] % P);
}
sc.close();
}
private static long fastpow(long a, long b) {
long res = 1;
long t = a;
while (b != 0) {
if (b % 2 == 1) {
res = res * t % P;
}
t = t * t % P;
b >>= 1;
}
return res;
}
}
如果直接用c(a / P, b / P) * c(a % P, b % P) % P,会无法通过用例10 10
3
因为这个会计算而3这个因子在模3的时候结果为0,而不是1
所以必须保证时(lucas(a / P, b / P) * c(a % P, b % P) %
P),才进行 计算
import java.util.*;
class Main {
static long P;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
long a = sc.nextLong(), b = sc.nextLong();
P = sc.nextInt();
System.out.println(lucas(a, b));
}
sc.close();
}
private static long lucas(long a, long b) {
if (a < P && b < P) {
return c(a, b);
}
//a / P不一定保证结果小于P,所以需要递归调用
return lucas(a / P, b / P) * c(a % P, b % P) % P;
}
private static long c(long a, long b) {
long res = 1;
for (long i = a - b + 1; i <= a; i++) {
res = res * i % P;
}
for (long i = 1; i <= b; i++) {
res = res * fastpow(i, P - 2) % P;
}
return res;
}
private static long fastpow(long a, long b) {
long res = 1;
long t = a;
while (b != 0) {
if (b % 2 == 1) {
res = res * t % P;
}
t = t * t % P;
b >>= 1;
}
return res;
}
}
import java.util.*;
class Main {
static List<Integer> primes;
static boolean[] flag;
static int N = 5010;
static int[] cnt;
static int size;
static List<Integer> res;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt(), b = sc.nextInt();
primes = new ArrayList<>();
flag = new boolean[N];
flag[0] = flag[1] = true;
getPrime();
size = primes.size();
cnt = new int[size];
res = new ArrayList<>();
res.add(1);
for (int i = 0; i < size; i++) {
int p = primes.get(i);
cnt[i] = getCnt(a, p) - getCnt(a - b, p) - getCnt(b, p);
for (int j = 0; j < cnt[i]; j++) {
mul(primes.get(i));
}
}
for (int i = res.size() - 1; i >= 0; i--) {
System.out.print(res.get(i));
}
sc.close();
}
private static void mul(int t) {
int pre = 0;
for (int i = 0; i < res.size(); i++) {
int c = res.get(i) * t + pre;
res.set(i, c % 10);
pre = c / 10;
}
//注意这里pre可能比较大,可能不止进位1次
while (pre != 0) {
res.add(pre % 10);
pre /= 10;
}
}
private static void getPrime() {
for (int i = 2; i < N; i++) {
if (!flag[i]) {
primes.add(i);
}
for (int j = 0; primes.get(j) * i < N; j++) {
flag[primes.get(j) * i] = true;
if (i % primes.get(j) == 0) {
break;
}
}
}
}
private static int getCnt(int x, int p) {
int res = 0;
int t = p;
while (x >= t) {
res += x / t;
t *= p;
}
return res;
}
/*试除法也能过
private static int getCnt(int x, int p) {
int res = 0;
for (int i = 2; i <= x; i++) {
int t = i;
while (t % p == 0) {
t /= p;
res++;
}
}
return res;
}
*/
}
不满足要求的路径数:也即一定会经过上图中的红线(y = x +
1),从第一次经过红线的点开始,一直到(6,6)这段路径,沿着红线进行对称,因为(6,6)点不会变,其关于红线(y
= x +
1)的对称点为(5,7)。所以每一条从(0,0)到(5,7)的路径,一定会经过红线,那么从第一次经过的点开始沿着红线对称,就会变成不满足要求的路径。其数量为
因此,所求路径数 = -
对于含有n个a和n个b的排列(满足任意前缀中,a一定多于或等于b,只有a和b两种情况)来说:
通分,化简后,可化为卡特兰数:
import java.util.*;
class Main {
static long P = (long) 1e9 + 7;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int N = 2 * n + 10;
long[] jc = new long[N];
jc[0] = jc[1] = 1;
for (int i = 2; i < N; i++) {
jc[i] = jc[i - 1] * i % P;
}
long res = jc[2 * n];
res = res * fastpow(jc[n], P - 2) % P;
res = res * fastpow(jc[n], P - 2) % P;
res = res * fastpow(n + 1, P - 2) % P;
System.out.println(res);
sc.close();
}
public static long fastpow(long x, long a) {
long res = 1;
long t = x;
while (a != 0) {
if (a % 2 == 1) {
res = res * t % P;
}
t = t * t % P;
a >>= 1;
}
return res;
}
}
import java.util.*;
class Main {
static int n, m;
static int[] primes;
static long res;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
primes = new int[m];
for (int i = 0; i < m; i++) {
primes[i] = sc.nextInt();
}
res = 0;
dfs(1, 0, 0);
System.out.println(res);
sc.close();
}
private static void dfs(long val, int pos, int cnt) {
if (pos >= m) {
if (val != 1) {
if (cnt % 2 == 1) {
res += n / val;
} else {
res -= n / val;
}
}
return;
}
//不提前中止的话,会爆long
if (val > n) {
return;
}
dfs(val * primes[pos], pos + 1, cnt + 1);
dfs(val, pos + 1, cnt);
}
}
用double,也可AC:
private static void dfs(double val, int pos, int cnt) {
if (pos >= m) {
if (val != 1) {
if (cnt % 2 == 1) {
res += (long)Math.floor(n / val);
} else {
res -= (long)Math.floor(n / val);
}
}
return;
}
dfs(val * primes[pos], pos + 1, cnt + 1);
dfs(val, pos + 1, cnt);
}
用位运算代替dfs:
枚举1 ~ ,每一个数代表一种情况
import java.util.*;
class Main {
static int n, m;
static int[] primes;
static long res;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
primes = new int[m];
for (int i = 0; i < m; i++) {
primes[i] = sc.nextInt();
}
res = 0;
int N = 1 << m;
for (int i = 1; i < N; i++) {
int cnt = 0;
long val = 1;
for (int j = 0; j < m; j++) {
int c = (i >> j) & 1;
if (c == 1) {
cnt++;
val *= primes[j];
if (val > n) {
break;
}
}
}
if (val > n) {
continue;
}
if (cnt % 2 == 1) {
res += n / val;
} else {
res -= n / val;
}
}
System.out.println(res);
sc.close();
}
}
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int res = 0;
for (int i = 0; i < n; i++) {
res ^= sc.nextInt();
}
System.out.println(res != 0 ? "Yes" : "No");
sc.close();
}
}
import java.util.*;
class Main {
static int[] S;
static int[] dp;
static int k;
static int M = 10010;
private static int sg(int x) {
if (dp[x] != -1) {
return dp[x];
}
Set<Integer> set = new HashSet<>();
for (int i = 0; i < k; i++) {
if (x >= S[i]) {
set.add(sg(x - S[i]));
}
}
int t;
for (t = 0; t < set.size(); t++) {
if (!set.contains(t)) {
break;
}
}
dp[x] = t;
return t;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
k = sc.nextInt();
S = new int[k];
dp = new int[M];
Arrays.fill(dp, -1);
dp[0] = 0;
for (int i = 0; i < k; i++) {
S[i] = sc.nextInt();
}
int n = sc.nextInt();
int res = 0;
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
res ^= sg(x);
}
System.out.println(res != 0 ? "Yes" : "No");
sc.close();
}
}
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int res = 0;
for (int i = 1; i <= n; i++) {
int x = sc.nextInt();
if (i % 2 == 1) {
res ^= x;
}
}
System.out.println(res != 0 ? "Yes" : "No");
sc.close();
}
}
graph LR;
A((x))
A --> B((SG: y1, z1)) --> G((...))
B --> H((...))
A --> C((SG: y2, z2))
A --> D((...))
A --> E((SG: yk, zk))
import java.util.*;
class Main {
static int[] dp;
private static int SG(int x) {
if (dp[x] != -1) {
return dp[x];
}
Set<Integer> set = new HashSet();
for (int i = 0; i < x; i++) {
//必须j <= i,为了防止a,b和b,a重复遍历
for (int j = 0; j <= i; j++) {
int t = SG(i) ^ SG(j);
set.add(t);
}
}
int i;
for (i = 0; i < set.size(); i++) {
if (!set.contains(i)) {
break;
}
}
dp[x] = i;
return i;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
dp = new int[110];
dp[0] = 0;
Arrays.fill(dp, -1);
int res = 0;
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
res ^= SG(x);
}
System.out.println(res != 0 ? "Yes" : "No");
sc.close();
}
}