# UVALive 4998 Simple Encryption --DFS

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define SMod 1000000000000
#define ll long long
using namespace std;
#define N 10007

ll K1,K2;
long long mul(long long a,long long b,long long mod) {
ll ite = (1LL<<20)-1;
return (a*(b>>20)%mod*(1ll<<20)%mod+a*(b&(ite))%mod)%mod;
}

ll fastm(ll a,ll b,ll m) {
ll res = 1LL;
while(b) {
if(b&1LL) res = mul(res,a,m);
a = mul(a,a,m);
b >>= 1;
}
return res;
}

ll wei[16],ans;

bool dfs(int c,ll now) {
if(c == 13) {
if(now >= wei[12]) { ans = now; return true; }
return false;
}
ll W = wei[c];
for(ll i=0;i<=9;i++) {
ll tmp = W*i+now;
if(fastm(K1,tmp,W) != tmp%W) continue;
if(dfs(c+1,tmp)) return true;
}
return false;
}

int main()
{
int t,cs = 1;
wei[0] = 1LL;
for(int i=1;i<=13;i++) wei[i] = wei[i-1]*10LL;
while(scanf("%lld",&K1)!=EOF && K1) {
dfs(0,0);
printf("Case %d: Public Key = %lld Private Key = %lld\n",cs++,K1,ans%SMod);
}
return 0;
}
View Code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define SMod 1000000000000
#define ll long long
using namespace std;

ll K1,K2;
long long mul(long long a,long long b,long long mod) {
ll ite = (1LL<<20)-1;
return (a*(b>>20)%mod*(1ll<<20)%mod+a*(b&(ite))%mod)%mod;
}

ll fastm(ll a,ll b,ll m) {
ll res = 1LL;
while(b) {
if(b&1LL) res = mul(res,a,m);
a = mul(a,a,m);
b >>= 1;
}
return res;
}
ll f(ll x) {
return fastm(K1,x,SMod);
}
ll gao(ll x) {
while(1) {
ll fx = f(x);
if(fx == x) return x;
x = fx;
}
}

int main()
{
int t,cs = 1;
while(scanf("%lld",&K1)!=EOF && K1) {
printf("Case %d: Public Key = %lld Private Key = %lld\n",cs++,K1,gao(1000000000007));
}
}
View Code

posted @ 2015-01-19 22:57 whatbeg 阅读(...) 评论(...) 编辑 收藏