关闭

poj 3708

163人阅读 评论(0) 收藏 举报
分类:

题目描述:

抽象一下就是:先给出一个100位的大整数.转换成d<100进制的数.然后有一个同样的目标数.d进制下位数要一样.每次转换i对应i位,最高位用a,低位用b.然后问最少转换几次能够到目标数.

题解:

第一步是高精度的进制转换.第二步是100个数,每个数求出来第一次转成功的次数,以及循环数(注意,感觉题目有毛病,其实就是第二次遇到目标数-第一次的次数就是循环数,竟然可能没有循环.标准的应该是随便找个循环就行).之后解线性同余模方程.

重点:

高精度转换+找循环和次数+线性同余模方程

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <ctype.h>
#include <limits.h>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
#define CLR(a) memset(a, 0, sizeof(a))
#define REP(i, a, b) for(ll i = a;i < b;i++)
#define REP_D(i, a, b) for(ll i = a;i <= b;i++)

typedef long long ll;

using namespace std;

struct P
{
    ll a, b;
};

const ll maxn = 100+10;
const ll eps = 5000;
ll res_m[maxn], res_k[maxn], ans[maxn], start[maxn];
char m[maxn], k[maxn];
ll d;
ll a[maxn], b[maxn];
ll vis[maxn], B[maxn], M[maxn];

ll gcd(ll a, ll b)
{
    if(b==0)
    {
        return a;
    }
    return gcd(b, a%b);
}
ll ext_gcd(ll a, ll b, ll &x, ll &y)
{
    if(b==0)
    {
        x = 1;
        y = 0;
        return a;
    }
    ll r = ext_gcd(b, a%b, x, y);
    ll t = x;
    x = y;
    y = t - (a/b*y);
    return r;
}

void trans(char s[], ll base1, ll base2, ll res[])//从高位开始除,答案保留在res中,并且是倒序
{
    //CLR(res);
    res[0] = 0;
    start[0] = strlen(s);
    REP(i, 0, start[0])
    {
        start[i+1] = s[i]-'0';
    }
    while(start[0] >= 1)
    {
        ll y = 0;
        ans[0] = start[0];
        REP_D(i, 1, start[0])
        {
            y = y*base1 + start[i];
            ans[i] = y/base2;
            y %= base2;
        }
        res[++res[0]] = y;
        ll i = 1;
        while(i <= ans[0]&&ans[i]==0)
        {
            i++;
        }
        start[0] = 0;
        while(i<=ans[0])
        {
            start[++start[0]] = ans[i];
            i++;
        }

    }
}

P mo_fangchengzu()//方程组
{
    ll x = 0, m=1;
    REP_D(i, 1, res_k[0])
    {
        ll a = m, b = ((B[i]-x)%M[i] + M[i])%M[i];
        ll d = gcd(a, M[i]);
        if(b%d!= 0)
        {
            return (P){0, -1};
        }
        else
        {
            ll t, temp;
            a /= d;
            b /= d;
            M[i] /= d;
            ext_gcd(a, M[i], t, temp);
            t = (t%M[i]+M[i])%M[i];
            t = (t*b)%M[i];
            x=x+m*t;
            m *= M[i];
            x %= m;
        }
    }
    return (P){x, m};
}

ll solve()//注意是ll...wa了好久这个地方
{
    trans(m, 10, d, res_m);
    trans(k, 10, d, res_k);
//    REP_D(i, 1, res_m[0])
//    {
//        prllf("%d ", res_m[i]);
//    }
//    prllf("\n");
    if(res_m[0]!=res_k[0])
    {
        return -1;
    }
    for(ll i = 1; i <= res_k[0]; i++)
    {
        //CLR(vis);
        ll from = res_m[i];
        ll to = res_k[i];
        //vis[from]=1;
        ll now = from;
        ll cnt = 0;
        ll ans = -1;
        while(1)
        {
            if(now == to&&ans==-1)//找循环
            {
                ans = cnt;
            }
            else if(now==to)
            {
                M[i] = cnt - ans;
                break;
            }

            //vis[now] = 1;
            cnt++;
            if(cnt > eps)
                return -1;
            if(i != res_k[0])
                now = b[now];
            else
                now = a[now];
        }
        //M[i] = cnt;
        if(ans==-1)
        {
            return -1;
        }
        B[i] = ans;
    }
    P out = mo_fangchengzu();
    if(out.b==-1)
    {
        return -1;
    }
    return out.a;
}


int main()
{
    //freopen("4Din.txt", "r", stdin);
    //freopen("4Dout.txt", "w", stdout);
    while(scanf("%I64d", &d) && d != -1)
    {
        REP_D(i, 1, d - 1)
        {
            scanf("%I64d", &a[i]);
        }
        REP_D(i, 0, d - 1)
        {
            scanf("%I64d", &b[i]);
        }
        scanf("%s%s", m, k);
        ll t = solve();
        if(t==-1)
        {
            printf("NO\n");
        }
        else
        {
            printf("%I64d\n", t);
        }
    }
    return 0;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:43542次
    • 积分:2405
    • 等级:
    • 排名:第15507名
    • 原创:206篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    最新评论