码迷,mamicode.com
首页 > 其他好文 > 详细

Uva 1069 Always an Integer ( 数学 )

时间:2014-10-30 22:25:01      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   os   ar   for   sp   

 

Uva 1069 Always an Integer ( 数学 )

 

bubuko.com,布布扣

 

bubuko.com,布布扣
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cctype>
using namespace std;
typedef long long LL;
#define MAXN 105
#define CLR( a, b ) memset( a, b, sizeof(a) )
LL c[ MAXN ], d;
char str[ MAXN * MAXN ];

void init()
{
    CLR( c, 0 );
    LL k, a, key, sign = 1;
    d = k = a = key = 0;
    int len = strlen( str );
    for( int i = 0; i <= len; ++i )
    {
        if( isdigit( str[i] ) )
        {
            if( key == 0 )        a = a * 10 + str[i] - 0;
            else if( key == 1 ) k = k * 10 + str[i] - 0;
            else if( key == 2 ) d = d * 10 + str[i] - 0;
        }
        else if( str[i] == / )    key = 2;
        else if( str[i] == n )    key = 1;
        else if( str[i] == - || str[i] == + || str[i] == ) )
        {
            if( key >= 1 )
            {
                if( k == 0 )    k = 1;
                if( a == 0 )    a = 1;
            }
            c[k] = a * sign;
            if( str[i] == - )    sign = -1;
            else sign = 1;
            
            key = a = k = 0;
        }
    }
}

LL fast_mod( LL a, int b, LL MOD )
{
    LL ans = 1;
    while( b )
    {
        if( b & 1 )    ans = ans * a % MOD;
        a = a * a % MOD;
        b >>= 1;
    }
    return ans;
}

bool judge()
{
    int n = MAXN - 1;
    while( c[n] == 0 ) n--;
    for( LL i = 1; i <= n + 1; ++i )
    {
        LL ans = 0;
        for( int j = 0; j <= n; ++j )
        {
            if( c[j] )
                ans = ( ans + c[j] * fast_mod( i, j, d ) ) % d;
            ans = ( ans + d ) % d;
        }
        if( ans )
            return false;
    }
    return true;
}

int main()
{
    int cas = 1;
    while(     ~scanf( "%s", str) && strcmp( ".", str ) != 0 )
    {
        init();
        printf( "Case %d: %s\n", cas++, judge() ? "Always an integer" : "Not always an integer" );
    }
    return 0;            
}
代码君

 

Uva 1069 Always an Integer ( 数学 )

标签:style   blog   http   io   color   os   ar   for   sp   

原文地址:http://www.cnblogs.com/BigBallon/p/4063626.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!