• Always an integer UVALive


    题目很简单,就是求表达式(P/D)的结果是不是整数。其中P是一个整系数的多项式,D是一个正整数。

    把1-k(最高次)+1都试一次就好了。结论可以总结归纳得到。(k取 0, 1, 2 .... 的情况推一次,可以推出结论)。

    // Asimple
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstdlib>
    #include <queue>
    #include <vector>
    #include <string>
    #include <cstring>
    #include <stack>
    #include <set>
    #include <map>
    #include <cmath>
    #define INF 0x3f3f3f3f
    #define mod 1000007
    #define debug(a) cout<<#a<<" = "<<a<<endl
    #define test() cout<<"============"<<endl
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    const int maxn = 500+5;
    ll n, m, T, len, cnt, num, ans, Max, k;
    string str;
    pair<ll, ll> p[maxn];
    
    ll qpow(ll a, ll b, ll md) {
        ll ans = 1;
        while( b ) {
            if( b&1 ) ans = (ans*a)%md;
            a = (a*a)%md;
            b >>= 1;
        }
        return ans;
    }
    
    void solve(int k) {
        cnt = 0;
        for(int i=1; str[i]!=')'; ) {
            ll first = 0;
            ll second = 0;
            bool fg = false;
            while( str[i]!='n' && str[i]!=')' ) {
                char ch = str[i];
                //debug(ch);
                if( ch==
                '+') { }
                else if( ch == '-' ) { fg = true; }
                else first = first*10 + (ch-'0');
                i ++; 
            }
            if( first == 0 ) first = 1;
            if( fg ) first *= -1;
            //debug(first);
            i ++;
            //printf("i==%d ch==%c
    ", i, str[i]);
            
            if( str[i]=='/' ) {
                second = 0;
                p[len++] = make_pair(first, second);
                break;
            } else if( str[i]!='^' ) { second = 1; }
            else if( str[i]=='^' ) {
                i ++;
                while( isdigit(str[i]) ) {
                    second = second*10+(str[i]-'0');
                    i ++;
                } 
            }
            //debug(second);
            p[len++] = make_pair(first, second);
            cnt = max(cnt, second);
            //debug(cnt);
            //debug(len);
        }
        
        m = 0;
        for(int i=0; i<k; i++) {
            if( str[i]=='/' ) {
                i ++;
                while( isdigit(str[i]) && i<k ) {
                    m = m*10 + (str[i]-'0' );
                    i ++;
                }
            }
        } 
    }
    
    void input(){
        int cas = 1;
        while( cin >> str ) {
            if( str[0] == '.' ) break;
            len = 0;
            k = str.length();
            solve(k);
            bool f = true;
            for(int i=1; i<=cnt+1; i++) {
                ll sum = 0;
                for(int j=0; j<len; j++) {
                    //printf("first==%d  second==%d
    ", p[j].first, p[j].second);
                    sum += (p[j].first*qpow(i, p[j].second, m))%m;
                    sum %= m;
                }
                if( sum ) {
                    f = false;
                    break;
                }
            }
            cout << "Case " << cas++ << ": ";
            if( f ) cout << "Always an integer" << endl;
            else cout << "Not always an integer" << endl;
        }
    }
    
    int main() {
        input();
        return 0;
    } 
  • 相关阅读:
    .Net Core主机配置
    .NET Core 初识
    控制反转IOC,依赖注入DI理解
    依赖倒置原则解析,理解面向抽象编程
    工厂模式
    IOC 概念
    利用Comparator排序
    使用Integer类实现二叉树排序
    先按成绩由高到低,相等则按年龄由低到高
    对象销毁之前进行某些操作
  • 原文地址:https://www.cnblogs.com/Asimple/p/7418010.html
Copyright © 2020-2023  润新知