Sha 1069 always an integer (Mathematics)

Source: Internet
Author: User

 

Sha 1069 always an integer (Mathematics)

 

 

#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;            }
Code Jun

 

Sha 1069 always an integer (Mathematics)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.