1076 K尾相等數
時間限制:500MS 內存限制:65536K
Description
從鍵盤輸入一個自然數K(99999999>K>1),若存在自然數M和N(M>N),使得K的M次方和K的N次方均大於或等於1000, 且它們的未尾三位數相等,則稱M和N是一對“K尾相等數”。請編程序,輸出K尾相等數中M+N最小值。
輸入樣例
2
輸出樣例
120
有點小開心嘻嘻,學習了一下快速冪取模,然後思路就來了-。-
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <string>
#include <stack>
#include <set>
#include <map>
#include <iostream>
#include <bitset>
#include <algorithm>
using namespace std;
#define MP make_pair
#define PB push_back
#define mst(a,b) memset((a),(b),sizeof(a))
#define TEST cout<<"*************************"<<endl
#define rep(s,n,up) for(int i = (s); i < (n); i+=(up))
#define per(n,e,down) for(int i = (n); i >= (e); i-=(down))
#define rep1(s,n,up) for(int j = (s); j < (n); j+=(up))
#define per1(n,e,down) for(int j = (n); j >= (e); j-=(down))
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> Pii;
typedef vector<int> Vi;
typedef vector<Pii> Vii;
const int inf = 0x3f3f3f3f;
const LL INF = (1uLL << 63) - 1;
const double Pi = acos(-1.0);
const int maxn = (1 << 16) + 7;
const uLL Hashmod = 29050993;
const double esp=1e-6;
//#define local
int quick(int a,int b,int c)
{
int ans=1;
a=a%c;
while(b){
if(b&1)ans=(ans*a)%c;
b>>=1;
a=(a*a)%c;
}
return ans;
}
int main() {
#ifdef local
freopen("input.txt", "r", stdin);
//freopen("output.txt","w",stdout);
#endif
//ios::sync_with_stdio(0);
//cin.tie();
int k,tail[1000]={0},start=0;
scanf("%d",&k);
for(int i=1;i<1000;i*=k)
++start;
for(int i=start;;++i){
int ans=quick(k,i,1000);
if(!tail[ans])
tail[ans]=i;
else{
printf("%d\n",i+tail[ans]);
break;
}
}
return 0;
}