您的位置:首页 > 其它

poj 3696 The Luckiest number(欧拉函数+快速幂取模)

2015-04-11 16:47 399 查看
Language:
Default

The Luckiest number

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 4561Accepted: 1222
Description

Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his own lucky number L. Now he wants to construct his luckiest number which is the minimum among all positive integers that are a multiple of L and
consist of only digit '8'.

Input

The input consists of multiple test cases. Each test case contains exactly one line containing L(1 ≤ L ≤ 2,000,000,000).

The last test case is followed by a line containing a zero.

Output

For each test case, print a line containing the test case number( beginning with 1) followed by a integer which is the length of Bob's luckiest number. If Bob can't construct his luckiest number, print a zero.

Sample Input
8
11
16
0

Sample Output
Case 1: 1
Case 2: 2
Case 3: 0


数论的题目 更多的还是需要分析啊。。。。。



#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define ll long long
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int Read()
{
    char ch;
    int a = 0;
    while((ch = getchar()) == ' ' | ch == '\n');
    a += ch - '0';
    while((ch = getchar()) != ' ' && ch != '\n')
    {
        a *= 10;
        a += ch - '0';
    }
    return a;
}

void Print(int a)    //输出外挂
{
     if(a>9)
         Print(a/10);
     putchar(a%10+'0');
}

ll L;
ll mod;
ll gcd(ll a,ll b)
{
    if(b==0)
        return a;
    return gcd(b,a%b);
}
ll mult(ll a,ll b)
{
    ll n=0;
    while(b)
    {
        if(b&1)
            n=(n+a)%mod;
        a=a*2%mod;
        b>>=1;
    }
    return n;
}
ll quick_mod(ll x)
{
    ll n=1,m=10;
    while(x)
    {
        if(x&1)
            n=mult(n,m);
        m=mult(m,m);
        x>>=1;
    }
    return n;
}

int main()
{
    //fread;
    int cs=1;
    while(scanf("%lld",&L)&&L)
    {
        printf("Case %d: ",cs++);
        mod=9*L/gcd(8,L);
        if(gcd(10,mod)!=1)
        {
            printf("0\n");
            continue;
        }
        ll rea=mod,n=mod;
        for(ll i=2;i*i<=n;i++)
            if(n%i==0)
        {
            rea=rea-rea/i;
            while(n%i==0)
                n/=i;
        }
        if(n>1)
            rea=rea-rea/n;//rea此时为m欧拉函数的值
        n=rea;
        ll p[100][2]; int cnt=0;
        for(ll i=2;i*i<=n;i++)
            if(n%i==0)
        {
            p[cnt][0]=i;
            p[cnt][1]=0;
            while(n%i==0)
            {
                n/=i;
                p[cnt][1]++;
            }
            cnt++;
        }
        if(n>1)
        {
            p[cnt][0]=n;
            p[cnt][1]=1;
            cnt++;
        }//p[i][0]存储n中素因子的值  p[i][1]存储该素因子的个数
        for(int i=0;i<cnt;i++)
        {
            for(int j=1;j<=p[i][1];j++)
                if(quick_mod(rea/p[i][0])==1)
                    rea/=p[i][0];
        }
        printf("%lld\n",rea);

    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: