publicclassMain { publicstaticvoidmain(String[] args) { Scannersc=newScanner(System.in); longa= sc.nextLong(), b = sc.nextLong(), n = sc.nextLong(); longweek= a * 5 + b * 2; //每周做题 longdays= (n / week) * 7; //周数*7天 n %= week; //余下不够一周的做题数 if (n <= a * 5) days += n / a + (n % a != 0 ? 1 : 0); //在周一到周五内 else { //周六和周日 days += 5; n -= a * 5; days += n / b + (n % b != 0 ? 1 : 0); } System.out.println(days); sc.close(); } }
Python 代码:
1 2 3 4 5 6 7 8 9 10 11
a, b, n = map(int, input().split()) week = a * 5 + b * 2#每周做题 days = (n // week) * 7#天数 n %= week #余数 if n <= a * 5: days += n // a + (n % a != 0) #在周一到周五内 else: #周六和周日 days += 5 n -= a * 5 days += n // b + (n % b != 0) print(days)
publicclassMain { staticlongfastPow(long a, long n, long mod) { longans=1; a %= mod; //重要,防止下面的ans*a越界 while (n > 0) { if ((n & 1) == 1) { ans = (ans * a) % mod; //取模 } a = (a * a) % mod; //取模 n >>= 1; } return ans; }
publicstaticvoidmain(String[] args) { Scannersc=newScanner(System.in); longb= sc.nextLong(), p = sc.nextLong(), k = sc.nextLong(); System.out.println(fastPow(b, p, k)); sc.close(); } }
Python 代码:
1 2 3 4 5 6 7 8 9 10 11 12
deffast_pow(a, n, mod): ans = 1 a %= mod #重要,防止下面的ans*a越界 while n > 0: if n & 1 == 1: ans = (ans * a) % mod #取模 a = (a * a) % mod #取模 n >>= 1 return ans
b, p, k = map(int, input().split()) print(fast_pow(b, p, k))
GCD 定义、性质
最大公约数 Greatest Common Divisor(GCD):整数 a 和 b 的 GCD 是指能同时整除 a 和 b 的最大整数,记为 gcd(a, b)。由于-a 的因子和 a 的因子相同,因此 gcd(a, b) = gcd(|a|, |b|)。编码时只关注正整数的最大公约数。 性质: (1)gcd(a, b) = gcd(a, a+b) = gcd(a, k·a+b) (2)gcd(ka, kb) = k·gcd(a, b) (3)定义多个整数的最大公约数:gcd(a, b, c) = gcd(gcd(a, b), c)。 (4)若 gcd(a, b) = d,则 gcd(a/d, b/d) = 1,即 a/d 与 b/d 互素。这个定理很重要。 (5)gcd(a+cb, b) = gcd(a, b)
#include<bits/stdc++.h> using namespace std; intlcm(int a, int b){ return a / __gcd(a, b) * b;} intmain(){ int a,b,c; cin>>a>>b>>c; int k = lcm(a,b); cout<<lcm(k,c)<<endl; return0; } import java.util.Scanner;
public classMain { staticintgcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
staticintlcm(int a, int b) { return a / gcd(a, b) * b; }
public staticvoidmain(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int k = lcm(a, b); System.out.println(lcm(k, c)); sc.close(); } } def gcd(a, b): return a if b == 0else gcd(b, a % b)
def lcm(a, b): return a // gcd(a, b) * b
a, b, c = map(int, input().split()) k = lcm(a, b) print(lcm(k, c))
5 素数的判断
素数定义:只能被 1 和自己整除的正整数。注:1 不是素数,最小素数是 2。
判断一个数 n 是不是素数:当n≤1014n≤1014时,用试除法;n > 1014 时,试除法不够用,需 要用高级算法,例如 Miller Rabin 算法。
试除法: 用[2,n-1]内的所有数去试着除 n,如果都不能整除,就是素数。 优化:把[2,n-1]缩小到[2,n][2,n]。证明:若 n = axb, 设 a≤nn ,则 b≥nn,如果 a 是 n 的因子,说明 n 不是素数,b 不用再试且 b 一定也是。
publicstaticvoidgetPrimes(int n) { Arrays.fill(bPrime, 0, n + 1, 0); for (inti=2; i <= n; i++) { if (bPrime[i] == 0) primes[cnt++] = i; for (intj=0; j < cnt && i * primes[j] <= n; j++) { bPrime[i * primes[j]] = 1; if (i % primes[j] == 0) break; } } }
Python 实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
N = 1000005 primes = [0] * N bprime = [False] * N cnt = 0
defgetPrimes(n: int): global cnt for i inrange(2, n+1): ifnot bprime[i]: primes[cnt] = i cnt += 1 j = 0 while j < cnt and i * primes[j] <= n: bprime[i * primes[j]] = True if i % primes[j] == 0: break j += 1
p = [0] * 20# p[] 记录因子,p[1] 是最小因子。一个 int 数的质因子最多有 10 几个 c = [0] * 40# c[i] 记录第 i 个因子的个数。一个因子的个数最多有 30 几个
deffactor(n): m = 0 for i inrange(2, int(math.sqrt(n)) + 1): if n % i == 0: m += 1 p[m], c[m] = i, 0 while n % i == 0: n //= i c[m] += 1 if n > 1: m += 1 p[m], c[m] = n, 1 return m
a, b = map(int, input().split()) for i inrange(a, b+1): m = factor(i) print(f'{i}=', end='') for j inrange(1, m+1): for k inrange(1, c[j]+1): print(p[j], end='') if k < c[j]: print('*', end='') if j < m: print('*', end='') print()