当前位置:   article > 正文

【算法笔记自学】第 5 章 入门篇(3)——数学问题

【算法笔记自学】第 5 章 入门篇(3)——数学问题

5.1简单数学

  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4. bool cmp(int a,int b){
  5. return a>b;
  6. }
  7. void to_array(int n,int num[]){
  8. for(int i=0;i<4;i++){
  9. num[i]=n%10;
  10. n /=10;
  11. }
  12. }
  13. int to_number(int num[]){
  14. int sum=0;
  15. for(int i=0;i<4;i++){
  16. sum=sum*10+num[i];
  17. }
  18. return sum;
  19. }
  20. int main(){
  21. int n,MIN,MAX;
  22. scanf("%d",&n);
  23. int num[5];
  24. while(1){
  25. to_array(n,num);
  26. sort(num,num+4);
  27. MIN=to_number(num);
  28. sort(num,num+4,cmp);
  29. MAX=to_number(num);
  30. n=MAX-MIN;
  31. printf("%04d-%04d=%04d\n",MAX,MIN,n);
  32. if(n==0||n==6174)break;
  33. }
  34. return 0;
  35. }

  1. #include <cstdio>
  2. #include <cmath>
  3. int main() {
  4. int a, b, c;
  5. scanf("%d%d%d", &a, &b, &c);
  6. int delta = b * b - 4 * a * c;
  7. if (delta < 0) {
  8. printf("No Solution");
  9. } else if (delta == 0) {
  10. printf("%.2f", -b / (2.0 * a));
  11. } else {
  12. printf("%.2f %.2f", (-b - sqrt((double)delta)) / (2.0 * a), (-b + sqrt((double)delta)) / (2.0 * a));
  13. }
  14. return 0;
  15. }

5.2最大公约数与最小公倍数

  1. #include <cstdio>
  2. #include <cmath>
  3. int gcd(int a,int b){
  4. if(b==0)return a;//求最大公约数的辗转相除法递归写法
  5. else return gcd(b,a%b);
  6. }
  7. int main() {
  8. int m,n;
  9. while(scanf("%d%d",&m,&n)!=EOF){
  10. printf("%d\n",gcd(m,n));
  11. }
  12. return 0;
  13. }

  1. #include <cstdio>
  2. int gcd(int a, int b) {
  3. if (b == 0) {
  4. return a;
  5. } else {
  6. return gcd(b, a % b);
  7. }
  8. }
  9. int main() {
  10. int a, b;
  11. scanf("%d%d", &a, &b);
  12. printf("%d", a / gcd(a, b) * b);
  13. return 0;
  14. }

5.3分数的四则运算

  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4. struct Fraction {
  5. int up, down;
  6. };
  7. int gcd(int a, int b) {
  8. if (b == 0) {
  9. return a;
  10. } else {
  11. return gcd(b, a % b);
  12. }
  13. }
  14. Fraction reduction(Fraction fraction) {
  15. if (fraction.down < 0) {
  16. fraction.up = -fraction.up;
  17. fraction.down = -fraction.down;
  18. }
  19. if (fraction.up == 0) {
  20. fraction.down = 1;
  21. } else {
  22. int d = gcd(abs(fraction.up), abs(fraction.down));
  23. fraction.up /= d;
  24. fraction.down /= d;
  25. }
  26. return fraction;
  27. }
  28. int main() {
  29. Fraction fraction;
  30. scanf("%d%d", &fraction.up, &fraction.down);
  31. Fraction result = reduction(fraction);
  32. if (result.down == 1) {
  33. printf("%d", result.up);
  34. } else {
  35. printf("%d %d", result.up, result.down);
  36. }
  37. return 0;
  38. }

  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4. struct Fraction {
  5. int up, down;
  6. };
  7. int gcd(int a, int b) {
  8. if (b == 0) {
  9. return a;
  10. } else {
  11. return gcd(b, a % b);
  12. }
  13. }
  14. Fraction reduction(Fraction fraction) {
  15. if (fraction.down < 0) {
  16. fraction.up = -fraction.up;
  17. fraction.down = -fraction.down;
  18. }
  19. if (fraction.up == 0) {
  20. fraction.down = 1;
  21. } else {
  22. int d = gcd(abs(fraction.up), abs(fraction.down));
  23. fraction.up /= d;
  24. fraction.down /= d;
  25. }
  26. return fraction;
  27. }
  28. Fraction add(Fraction f1, Fraction f2) {
  29. Fraction result;
  30. result.up = f1.up * f2.down + f2.up * f1.down;
  31. result.down = f1.down * f2.down;
  32. return reduction(result);
  33. }
  34. int main() {
  35. Fraction f1, f2;
  36. scanf("%d%d%d%d", &f1.up, &f1.down, &f2.up, &f2.down);
  37. Fraction result = add(f1, f2);
  38. if (result.down == 1) {
  39. printf("%d", result.up);
  40. } else {
  41. printf("%d %d", result.up, result.down);
  42. }
  43. return 0;
  44. }

  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4. struct Fraction {
  5. int up, down;
  6. };
  7. int gcd(int a, int b) {
  8. if (b == 0) {
  9. return a;
  10. } else {
  11. return gcd(b, a % b);
  12. }
  13. }
  14. Fraction reduction(Fraction fraction) {
  15. if (fraction.down < 0) {
  16. fraction.up = -fraction.up;
  17. fraction.down = -fraction.down;
  18. }
  19. if (fraction.up == 0) {
  20. fraction.down = 1;
  21. } else {
  22. int d = gcd(abs(fraction.up), abs(fraction.down));
  23. fraction.up /= d;
  24. fraction.down /= d;
  25. }
  26. return fraction;
  27. }
  28. Fraction sub(Fraction f1, Fraction f2) {
  29. Fraction result;
  30. result.up = f1.up * f2.down - f2.up * f1.down;
  31. result.down = f1.down * f2.down;
  32. return reduction(result);
  33. }
  34. int main() {
  35. Fraction f1, f2;
  36. scanf("%d%d%d%d", &f1.up, &f1.down, &f2.up, &f2.down);
  37. Fraction result = sub(f1, f2);
  38. if (result.down == 1) {
  39. printf("%d", result.up);
  40. } else {
  41. printf("%d %d", result.up, result.down);
  42. }
  43. return 0;
  44. }

  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4. struct Fraction {
  5. int up, down;
  6. };
  7. int gcd(int a, int b) {
  8. if (b == 0) {
  9. return a;
  10. } else {
  11. return gcd(b, a % b);
  12. }
  13. }
  14. Fraction reduction(Fraction fraction) {
  15. if (fraction.down < 0) {
  16. fraction.up = -fraction.up;
  17. fraction.down = -fraction.down;
  18. }
  19. if (fraction.up == 0) {
  20. fraction.down = 1;
  21. } else {
  22. int d = gcd(abs(fraction.up), abs(fraction.down));
  23. fraction.up /= d;
  24. fraction.down /= d;
  25. }
  26. return fraction;
  27. }
  28. Fraction multiply(Fraction f1, Fraction f2) {
  29. Fraction result;
  30. result.up = f1.up * f2.up;
  31. result.down = f1.down * f2.down;
  32. return reduction(result);
  33. }
  34. int main() {
  35. Fraction f1, f2;
  36. scanf("%d%d%d%d", &f1.up, &f1.down, &f2.up, &f2.down);
  37. Fraction result = multiply(f1, f2);
  38. if (result.down == 1) {
  39. printf("%d", result.up);
  40. } else {
  41. printf("%d %d", result.up, result.down);
  42. }
  43. return 0;
  44. }

  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4. struct Fraction {
  5. int up, down;
  6. };
  7. int gcd(int a, int b) {
  8. if (b == 0) {
  9. return a;
  10. } else {
  11. return gcd(b, a % b);
  12. }
  13. }
  14. Fraction reduction(Fraction fraction) {
  15. if (fraction.down < 0) {
  16. fraction.up = -fraction.up;
  17. fraction.down = -fraction.down;
  18. }
  19. if (fraction.up == 0) {
  20. fraction.down = 1;
  21. } else {
  22. int d = gcd(abs(fraction.up), abs(fraction.down));
  23. fraction.up /= d;
  24. fraction.down /= d;
  25. }
  26. return fraction;
  27. }
  28. Fraction div(Fraction f1, Fraction f2) {
  29. Fraction result;
  30. result.up = f1.up * f2.down;
  31. result.down = f1.down * f2.up;
  32. return reduction(result);
  33. }
  34. int main() {
  35. Fraction f1, f2;
  36. scanf("%d%d%d%d", &f1.up, &f1.down, &f2.up, &f2.down);
  37. Fraction result = div(f1, f2);
  38. if(!f2.up){
  39. printf("undefined");
  40. }
  41. else if (result.down == 1) {
  42. printf("%d", result.up);
  43. } else {
  44. printf("%d %d", result.up, result.down);
  45. }
  46. return 0;
  47. }

5.4素数 

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cmath>
  4. using namespace std;
  5. bool isPrime(int n){
  6. if(n<=1)return false;
  7. int sqr=(int)sqrt(1.0*n);
  8. for(int i=2;i<=sqr;i++){
  9. if(n%i==0)return false;
  10. }
  11. return true;
  12. }
  13. int main() {
  14. int n;
  15. scanf("%d",&n);
  16. if(isPrime(n))printf("Yes");
  17. else printf("No");
  18. return 0;
  19. }

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cmath>
  4. using namespace std;
  5. bool isPrime(int n){
  6. if(n<=1)return false;
  7. int sqr=(int)sqrt(1.0*n);
  8. for(int i=2;i<=sqr;i++){
  9. if(n%i==0)return false;
  10. }
  11. return true;
  12. }
  13. int main() {
  14. int n;
  15. scanf("%d",&n);
  16. for(int i=1;i<n+1;i++)
  17. {
  18. if(isPrime(i))printf("%d\n",i);
  19. }
  20. return 0;
  21. }

5.5质因子分解

  1. #include <cstdio>
  2. int main() {
  3. int n;
  4. scanf("%d", &n);
  5. int counter = 0;
  6. while (n % 2 == 0) {
  7. counter++;
  8. n /= 2;
  9. }
  10. printf("%d", counter);
  11. return 0;
  12. }

  1. #include <cstdio>
  2. #include <cmath>
  3. #include <cstring>
  4. #include <vector>
  5. using namespace std;
  6. const int MAXN = 1000 + 1;
  7. bool isPrime[MAXN];
  8. vector<int> primes;
  9. void getPrimes(int n) {
  10. memset(isPrime, true, sizeof(isPrime));
  11. for (int i = 2; i <= n; i++) {
  12. if (isPrime[i]) {
  13. primes.push_back(i);
  14. for (int j = i + i; j <= n; j += i) {
  15. isPrime[j] = false;
  16. }
  17. }
  18. }
  19. }
  20. int main() {
  21. int n;
  22. scanf("%d", &n);
  23. getPrimes((int)sqrt(1.0 * n));
  24. for (int i = 0; i < primes.size() && n > 1; i++) {
  25. int counter = 0;
  26. while (n > 1 && n % primes[i] == 0) {
  27. counter++;
  28. n /= primes[i];
  29. }
  30. if (counter > 0) {
  31. printf("%d %d\n", primes[i], counter);
  32. }
  33. }
  34. if (n > 1) {
  35. printf("%d 1", n);
  36. }
  37. return 0;
  38. }

5.6大整数运算 

  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5. using namespace std;
  6. typedef vector<int> BigInt;
  7. BigInt toBigInt(string nums) {
  8. BigInt result;
  9. for (int i = (int)nums.length() - 1; i >= 0; i--) {
  10. result.push_back(nums[i] - '0');
  11. }
  12. return result;
  13. }
  14. int compare(BigInt a, BigInt b) {
  15. if (a.size() > b.size()) {
  16. return 1;
  17. } else if (a.size() < b.size()) {
  18. return -1;
  19. } else {
  20. for (int i = (int)a.size() - 1; i >= 0; i--) {
  21. if (a[i] > b[i]) {
  22. return 1;
  23. } else if (a[i] < b[i]) {
  24. return -1;
  25. }
  26. }
  27. return 0;
  28. }
  29. }
  30. int main() {
  31. string nums1, nums2;
  32. cin >> nums1 >> nums2;
  33. BigInt a = toBigInt(nums1);
  34. BigInt b = toBigInt(nums2);
  35. int compareResult = compare(a, b);
  36. if (compareResult < 0) {
  37. printf("a < b");
  38. } else if (compareResult > 0) {
  39. printf("a > b");
  40. } else {
  41. printf("a = b");
  42. }
  43. }

  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5. using namespace std;
  6. typedef vector<int> BigInt;
  7. BigInt toBigInt(string nums) {
  8. BigInt result;
  9. for (int i = (int)nums.length() - 1; i >= 0; i--) {
  10. result.push_back(nums[i] - '0');
  11. }
  12. return result;
  13. }
  14. BigInt add(BigInt a, BigInt b) {
  15. BigInt c;
  16. int carry = 0;
  17. for (int i = 0; i < a.size() || i < b.size(); i++) {
  18. int aDigit = i < a.size() ? a[i] : 0;
  19. int bDigit = i < b.size() ? b[i] : 0;
  20. int sum = aDigit + bDigit + carry;
  21. c.push_back(sum % 10);
  22. carry = sum / 10;
  23. }
  24. if (carry) {
  25. c.push_back(carry);
  26. }
  27. return c;
  28. }
  29. void print(BigInt a) {
  30. for (int i = (int)a.size() - 1; i >= 0; i--) {
  31. cout << a[i];
  32. }
  33. }
  34. int main() {
  35. string nums1, nums2;
  36. cin >> nums1 >> nums2;
  37. BigInt a = toBigInt(nums1);
  38. BigInt b = toBigInt(nums2);
  39. print(add(a, b));
  40. return 0;
  41. }

  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5. using namespace std;
  6. typedef vector<int> BigInt;
  7. BigInt toBigInt(string nums) {
  8. BigInt result;
  9. for (int i = (int)nums.length() - 1; i >= 0; i--) {
  10. result.push_back(nums[i] - '0');
  11. }
  12. return result;
  13. }
  14. int compare(BigInt a, BigInt b) {
  15. if (a.size() > b.size()) {
  16. return 1;
  17. } else if (a.size() < b.size()) {
  18. return -1;
  19. } else {
  20. for (int i = (int)a.size() - 1; i >= 0; i--) {
  21. if (a[i] > b[i]) {
  22. return 1;
  23. } else if (a[i] < b[i]) {
  24. return -1;
  25. }
  26. }
  27. return 0;
  28. }
  29. }
  30. BigInt sub(BigInt a, BigInt b) {
  31. BigInt c;
  32. for (int i = 0; i < a.size() || i < b.size(); i++) {
  33. int bDigit = i < b.size() ? b[i] : 0;
  34. if (a[i] < bDigit) {
  35. a[i + 1]--;
  36. a[i] += 10;
  37. }
  38. c.push_back(a[i] - bDigit);
  39. }
  40. while (c.size() > 1 && c.back() == 0) {
  41. c.pop_back();
  42. }
  43. return c;
  44. }
  45. void print(BigInt a) {
  46. for (int i = (int)a.size() - 1; i >= 0; i--) {
  47. cout << a[i];
  48. }
  49. }
  50. int main() {
  51. string nums1, nums2;
  52. cin >> nums1 >> nums2;
  53. BigInt a = toBigInt(nums1);
  54. BigInt b = toBigInt(nums2);
  55. if (compare(a, b) >= 0) {
  56. print(sub(a, b));
  57. } else {
  58. cout << "-";
  59. print(sub(b, a));
  60. }
  61. return 0;
  62. }

  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5. using namespace std;
  6. typedef vector<int> BigInt;
  7. BigInt toBigInt(string nums) {
  8. BigInt result;
  9. for (int i = (int)nums.length() - 1; i >= 0; i--) {
  10. result.push_back(nums[i] - '0');
  11. }
  12. return result;
  13. }
  14. int compare(BigInt a, BigInt b) {
  15. if (a.size() > b.size()) {
  16. return 1;
  17. } else if (a.size() < b.size()) {
  18. return -1;
  19. } else {
  20. for (int i = (int)a.size() - 1; i >= 0; i--) {
  21. if (a[i] > b[i]) {
  22. return 1;
  23. } else if (a[i] < b[i]) {
  24. return -1;
  25. }
  26. }
  27. return 0;
  28. }
  29. }
  30. BigInt mul(BigInt a, int b) {
  31. BigInt c;
  32. int carry=0;;
  33. for (int i = 0; i < a.size(); i++) {
  34. int temp=a[i]*b+carry;
  35. c.push_back(temp%10);
  36. carry=temp/10;
  37. }
  38. while(carry!=0){
  39. c.push_back(carry%10);
  40. carry/=10;
  41. }
  42. while (c.size() > 1 && c.back() == 0) {
  43. c.pop_back();
  44. }
  45. return c;
  46. }
  47. void print(BigInt a) {
  48. for (int i = (int)a.size() - 1; i >= 0; i--) {
  49. cout << a[i];
  50. }
  51. }
  52. int main() {
  53. string nums;
  54. int b;
  55. cin >> nums >> b;
  56. BigInt a = toBigInt(nums);
  57. print(mul(a, b));
  58. return 0;
  59. return 0;
  60. }

  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5. using namespace std;
  6. typedef vector<int> BigInt;
  7. BigInt toBigInt(string nums) {
  8. BigInt result;
  9. for (int i = (int)nums.length() - 1; i >= 0; i--) {
  10. result.push_back(nums[i] - '0');
  11. }
  12. return result;
  13. }
  14. BigInt mul(BigInt a, BigInt b) {
  15. BigInt c = BigInt(a.size() + b.size() + 1, 0);
  16. for (int i = 0; i < a.size(); i++) {
  17. for (int j = 0; j < b.size(); j++) {
  18. c[i + j] += a[i] * b[j];
  19. }
  20. }
  21. for (int i = 0; i < a.size() + b.size(); i++) {
  22. if (c[i] >= 10) {
  23. c[i + 1] += c[i] / 10;
  24. c[i] = c[i] % 10;
  25. }
  26. }
  27. while (c.size() > 1 && c.back() == 0) {
  28. c.pop_back();
  29. }
  30. return c;
  31. }
  32. void print(BigInt a) {
  33. for (int i = (int)a.size() - 1; i >= 0; i--) {
  34. cout << a[i];
  35. }
  36. }
  37. int main() {
  38. string nums1, nums2;
  39. cin >> nums1 >> nums2;
  40. BigInt a = toBigInt(nums1);
  41. BigInt b = toBigInt(nums2);
  42. print(mul(a, b));
  43. return 0;
  44. }

  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5. using namespace std;
  6. typedef vector<int> BigInt;
  7. BigInt toBigInt(string nums) {
  8. BigInt result;
  9. for (int i = (int)nums.length() - 1; i >= 0; i--) {
  10. result.push_back(nums[i] - '0');
  11. }
  12. return result;
  13. }
  14. BigInt div(BigInt a, int b, int &r) {
  15. BigInt c;
  16. for (int i = (int)a.size() - 1; i >= 0; i--) {
  17. r = r * 10 + a[i];
  18. c.push_back(r / b);
  19. r = r % b;
  20. }
  21. reverse(c.begin(), c.end());
  22. while (c.size() > 1 && c.back() == 0) {
  23. c.pop_back();
  24. }
  25. return c;
  26. }
  27. void print(BigInt a) {
  28. for (int i = (int)a.size() - 1; i >= 0; i--) {
  29. cout << a[i];
  30. }
  31. }
  32. int main() {
  33. string nums;
  34. int b, r = 0;
  35. cin >> nums >> b;
  36. if (b == 0) {
  37. cout << "undefined";
  38. return 0;
  39. }
  40. BigInt a = toBigInt(nums);
  41. BigInt q = div(a, b, r);
  42. print(q);
  43. cout << " " << r;
  44. return 0;
  45. }

5.7扩展欧几里得算法

  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4. int gcd(int a, int b) {
  5. if (b == 0) {
  6. return a;
  7. } else {
  8. return gcd(b, a % b);
  9. }
  10. }
  11. int main() {
  12. int a, b, c;
  13. scanf("%d%d%d", &a, &b, &c);
  14. printf(c % gcd(a, b) == 0 ? "Yes" : "No");
  15. return 0;
  16. }

  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4. int exGcd(int a, int b, int &x, int &y) {
  5. if (b == 0) {
  6. x = 1;
  7. y = 0;
  8. return a;
  9. }
  10. int d = exGcd(b, a % b, x, y);
  11. int temp = x;
  12. x = y;
  13. y = temp - a / b * y;
  14. return d;
  15. }
  16. int main() {
  17. int a, b, x, y;
  18. scanf("%d%d", &a, &b);
  19. int d = exGcd(a, b, x, y);
  20. int step = b / d;
  21. int minX = (x % step + step) % step;
  22. printf("%d %d", minX, (d - a * minX) / b);
  23. return 0;
  24. }

  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4. int exGcd(int a, int b, int &x, int &y) {
  5. if (b == 0) {
  6. x = 1;
  7. y = 0;
  8. return a;
  9. }
  10. int d = exGcd(b, a % b, x, y);
  11. int temp = x;
  12. x = y;
  13. y = temp - a / b * y;
  14. return d;
  15. }
  16. int solve(int a, int b, int c) {
  17. int x, y;
  18. int d = exGcd(a, b, x, y);
  19. if (c % d) {
  20. return -1;
  21. } else {
  22. int step = abs(b / d);
  23. int minX = (c * x / d % step + step) % step;
  24. return minX;
  25. }
  26. }
  27. int main() {
  28. int a, b, c;
  29. scanf("%d%d%d", &a, &b, &c);
  30. int minX = solve(a, b, c);
  31. if (minX == -1) {
  32. printf("No Solution");
  33. } else {
  34. printf("%d %d", minX, (c - a * minX) / b);
  35. }
  36. return 0;
  37. }

  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4. int exGcd(int a, int b, int &x, int &y) {
  5. if (b == 0) {
  6. x = 1;
  7. y = 0;
  8. return a;
  9. }
  10. int d = exGcd(b, a % b, x, y);
  11. int temp = x;
  12. x = y;
  13. y = temp - a / b * y;
  14. return d;
  15. }
  16. int solve(int a, int b, int c) {
  17. int x, y;
  18. int d = exGcd(a, b, x, y);
  19. if (c % d) {
  20. return -1;
  21. } else {
  22. int step = abs(b / d);
  23. int minX = (c * x / d % step + step) % step;
  24. return minX;
  25. }
  26. }
  27. int main() {
  28. int a, c, m, x, y;
  29. scanf("%d%d%d", &a, &c, &m);
  30. int minX = solve(a, m, c);
  31. if (minX == -1) {
  32. printf("No Solution");
  33. } else {
  34. printf("%d", minX);
  35. }
  36. return 0;
  37. }

  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4. int exGcd(int a, int b, int &x, int &y) {
  5. if (b == 0) {
  6. x = 1;
  7. y = 0;
  8. return a;
  9. }
  10. int d = exGcd(b, a % b, x, y);
  11. int temp = x;
  12. x = y;
  13. y = temp - a / b * y;
  14. return d;
  15. }
  16. int invert(int a, int m) {
  17. int x, y;
  18. int d = exGcd(a, m, x, y);
  19. if (d != 1) {
  20. return -1;
  21. } else {
  22. return (x % m + m) % m;
  23. }
  24. }
  25. int main() {
  26. int a, m;
  27. scanf("%d%d", &a, &m);
  28. int result = invert(a, m);
  29. if (result == -1) {
  30. printf("No Solution");
  31. } else {
  32. printf("%d", result);
  33. }
  34. return 0;
  35. }

  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4. int exGcd(int a, int b, int &x, int &y) {
  5. if (b == 0) {
  6. x = 1;
  7. y = 0;
  8. return a;
  9. }
  10. int d = exGcd(b, a % b, x, y);
  11. int temp = x;
  12. x = y;
  13. y = temp - a / b * y;
  14. return d;
  15. }
  16. int invert(int a, int m) {
  17. int x, y;
  18. int d = exGcd(a, m, x, y);
  19. if (d != 1) {
  20. return -1;
  21. } else {
  22. return (x % m + m) % m;
  23. }
  24. }
  25. int main() {
  26. int n, a, m, b;
  27. scanf("%d%d%d", &n, &a, &m);
  28. int result = invert(abs(a), m);
  29. for (int i = 0; i < n; i++) {
  30. scanf("%d", &b);
  31. result = (result * b) % m;
  32. }
  33. printf("%d", result);
  34. return 0;
  35. }

5.8组合数

  1. #include <cstdio>
  2. int cal(int n,int p)
  3. {
  4. if(n<p)return 0;
  5. return n/p+cal(n/p,p);
  6. }
  7. int main() {
  8. int n,p=2;
  9. scanf("%d", &n);
  10. printf("%d", cal(n,p));
  11. return 0;
  12. }

  1. #include <cstdio>
  2. typedef long long LL;
  3. LL C(LL n, LL m) {
  4. LL ans = 1;
  5. for (LL i = 1; i <= m; i++) {
  6. ans = ans * (n - m + i) / i;
  7. }
  8. return ans;
  9. }
  10. int main() {
  11. LL n, m;
  12. scanf("%lld%lld", &n, &m);
  13. printf("%lld", C(n, m));
  14. return 0;
  15. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/817527
推荐阅读
相关标签
  

闽ICP备14008679号