6 条题解

  • 4
    @ 2023-6-25 2:26:32

    使用快速幂算法计算a(分之b)取模1000的值。但是,在最后输出结果时,应该使用%03lld,而不是%03d。其中,%03lld表示输出一个3位的长整数,若长度不足则在前面补0。

    另外,为了避免数据溢出,可以将s对1000取模,即:

    for (int i = 0; i < b; i++) {
        s *= a;
        s %= 1000;
    }
    

    上述代码中,每次计算完s×a之后,都会对1000取模,保证s始终小于或等于1000。

    完整代码如下:

    #include <iostream>
    #include <cstdio>
    using namespace std;
      
    long long a, b, s = 1;
     
    int main()
    {
        cin >> a >> b;
        for(int i = 0;i < b;++i)
            s *= a, s %= 1000; 
        printf("%03lld", s); 
        return 0;
    }
    
    • 2
      @ 2024-2-5 17:12:57

      1.不能用pow()(数据范围大,函数返回数据类型为double)

      所以只能用循环实现幂的运算

      2.求余运算需要每次循环进行一次(根据求余分配律... 因为数据范围太大)

      #include <bits/stdc++.h>
      using namespace std;
      int main(){
      	int a,b;
      	cin >> a >> b;
      	int sum = 1; //
      	for (int i = 1; i <= b; i++)
      	{
      		sum *= a; 
      		sum %= 1000; // 每次进行求余
      	}
      	printf("%03d", sum); // 补足三位
      }
      
      • 2
        @ 2023-10-29 16:02:19
        #include <iostream>
        using namespace std;
        int main() {
        int a, b;
        cin >> a >> b;
        
        int result = 1;
        for (int i = 0; i < b; i++) {
        result = (result * a) % 1000;
        }
        
        cout << result / 100 << result / 10 % 10 << result % 10 << std::endl;
        
        return 0;
        }
        
        • 2
          @ 2023-7-22 18:42:15
          #include <iostream>
          #include <cstdio>
          using namespace std;
          long long a, b, s = 1;
          int main()
          {
              cin >> a >> b;
              for(int i = 0;i < b;++i)
                  s *= a,s %= 1000;
              if(10<=s&&99>=s) cout<<"0";
              if(0<=s&&9>=s) cout<<"00";
              cout<<s;
              return 0;
          }
          
          • 2
            @ 2023-6-22 19:26:17
            #include<bits/stdc++.h>
            using namespace std;
            int main()
            {
                int a,b,t=1;
                cin>>a>>b;
                for (int i=1;i<=b;i++)
                {
                    t=t*a%1000;
                }
                cout <<setw(3)<<t;//取末尾三位数
                return 0;
            }
            
            • 1
              @ 2024-6-2 14:58:25
              #include <bits/stdc++.h>
              using namespace std;
              int main()
              {
                  int a,b;
                  int po=1;
                  cin >> a>>b;
                  for(int i = 1;i<=b;i++)
                  {
                      po *=a;
                      po %=1000;
                  }
                  printf("%03d",po);
              
                  return 0;
              }
              
              • 1

              信息

              ID
              181
              时间
              1000ms
              内存
              128MiB
              难度
              4
              标签
              递交数
              202
              已通过
              88
              上传者