2 solutions

  • 0
    @ 2022-12-27 20:12:00

    算法很容易想到,但是就是范围太搞人了。本人技术欠佳,想不到更优秀的算法了。只有拿更大的数据类型来存,避免了这个问题。(仅限本题目的情况,刚好卡在这个测试数据范围,其它超出数据范围的一概不知)

    #include <bits/stdc++.h>
    
    long long func(__int128 q, __int128 n, __int128 mod)
    {
    	__int128 ans = 1,a = q % mod;
    	while(n)
    	{
    		if(n & 1)
    		{
    			ans = ans * a % mod;
    		}
    		a = a * a % mod;
    		n >>= 1;
    	}
    	return ans;
    }
    
    int main()
    {
    	long long a, b, c;
    	while(~scanf("%lld %lld %lld", &a, &b, &c))
    	{
    		printf("%lld\n",func(a,b,c));
    	}
    }
    
    • 0
      @ 2022-4-3 12:01:26

      蒙对

      #include <bits/stdc++.h>
      using namespace std;
      
      unsigned long long check(unsigned long long a,unsigned long long b,unsigned long long c){//龟速乘 (a * b) % c
          unsigned long long ret = 0;
          while(b > 0){
              if(b & 1){
                  ret = (ret % c + a % c) % c;
              }
              a = (a % c + a % c) % c;
              b = b >> 1; 
          }
          return ret;
      }
      
      
      int main(){
          std::ios::sync_with_stdio(false);
          unsigned long long a,b,c,sum = 1;
          while(cin>>a>>b>>c){
              sum = 1;
              while(b > 0){
                  if(b & 1){
                      sum = check(sum % c,a % c,c);
                  }
                  a = check(a % c,a % c,c);
                  b = b >> 1;
              }
              cout<<sum<<endl;
          }
          return 0;
      }
      
      • 1

      Information

      ID
      1551
      Time
      1000ms
      Memory
      256MiB
      Difficulty
      8
      Tags
      # Submissions
      155
      Accepted
      21
      Uploaded By