Advertisement
wojiaocbj

Untitled

Mar 23rd, 2023
663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.69 KB | None | 0 0
  1. /*
  2.  Author: 曹北健(37509)
  3.  Result: AC Submission_id: 5274020
  4.  Created at: Thu Mar 23 2023 21:03:16 GMT+0800 (China Standard Time)
  5.  Problem: 6756  Time: 26    Memory: 1592
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <math.h>
  12. #include <ctype.h>
  13. #include <time.h>
  14. #pragma warning(disable:4996 6031)
  15. long long get_phi(long long n){
  16.     long long phi = n, i;
  17.     for(i = 2; i * i <= n; i++){
  18.         if(n % i == 0){
  19.             while(n % i == 0)
  20.                 n /= i;
  21.             phi -= phi / i;
  22.         }
  23.     }
  24.     if(n > 1)
  25.         phi -= phi / n;
  26.     return phi;
  27. }
  28. long long gcd(long long a, long long b){
  29.     return b ? gcd(b, a % b) : a;
  30. }
  31. long long pow_mod(long long a, long long t, long long p){
  32.     long long ans = 1;
  33.     while(t--)
  34.         ans = 1LL * ans * a % p;
  35.     return ans;
  36. }
  37. int main(){
  38. #ifdef _DEBUG
  39.     freopen("../../input.txt", "r", stdin);
  40. #endif // _DEBUG
  41.     //不用开数组 因为你不知道要开多大
  42.     //如果再让我debug时候看到 开数组的时候 int/double/whatever a[n]其中n是个变量 直接弹脑壳
  43.     int T;
  44.     scanf("%d", &T);
  45.     long long a, t, p, g, ph;
  46.     while(T--){
  47.         scanf("%lld%lld%lld", &a, &t, &p);
  48.         g = gcd(a, p);
  49.         ph = get_phi(p);
  50.         if(g == 1){
  51.             printf("Case #1: %lld\n", pow_mod(a, t % ph, p));
  52.         }
  53.         else{
  54.             if(t < ph){
  55.                 printf("Case #2: %lld\n", pow_mod(a, t, p));
  56.             }
  57.             else{
  58.                 printf("Case #3: %lld\n", pow_mod(a, (t % ph) + ph, p));
  59.             }
  60.         }
  61.     }
  62. #ifdef _DEBUG
  63.     freopen("CON", "r", stdin);
  64.     system("pause");
  65. #endif // _DEBUG
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement