数论练习(3)——相同后三位(快速幂)

3085 相同的后三位

 

 时间限制: 1 s
 空间限制: 256000 KB
 题目等级 : 青铜 Bronze
 
 
 
题目描述 Description

对于给定的p,编写程序求最小正整数m,n(0<n<m)为何值时,pm与pn的最后三位数字相同。

输入描述 Input Description

一个正整数p(100≤p≤9999)

输出描述 Output Description

一行,最小的m和n,m和n用空格隔开。如有多组,输出最小的m那组。

样例输入 Sample Input

100

样例输出 Sample Output

3 2

数据范围及提示 Data Size & Hint

100≤p≤9999

#include<bits/stdc++.h>
using namespace std;
typedef long long  ll;

const int inf = 0x3f3f3f3f;
const int maxn = 40000 + 20;
const int moder = 1e9 + 7;
const int K = 256;

ll mod_pow(ll x,ll n,ll mod)
{
    ll res = 1;
    while(n > 0)
    {
        if(n & 1) res = (res * x) % mod;
        x = (x * x) % mod;
        n >>= 1;
    }
    return res;
}

int main()
{
    ll a;
    scanf("%lld",&a);
    for(int m=2; ;m++)
    {
        ll ans1 = mod_pow(a,m,1000);
        for(int n=1;n < m;n++)
        {
            ll ans2 = mod_pow(a,n,1000);
            if(ans1 == ans2)
            {
                cout << m << " " << n << endl;
                goto jiba;
            }
        }
    }

    jiba:
    return 0;
}

水题就是舒服啊!

原文地址:https://www.cnblogs.com/cunyusup/p/8401895.html