Always an integer UVALive - 4119

时间:2023-03-10 07:05:36
Always an integer UVALive - 4119

题目很简单,就是求表达式(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 = +;
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 = ;
while( b ) {
if( b& ) ans = (ans*a)%md;
a = (a*a)%md;
b >>= ;
}
return ans;
} void solve(int k) {
cnt = ;
for(int i=; str[i]!=')'; ) {
ll first = ;
ll second = ;
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* + (ch-'');
i ++;
}
if( first == ) first = ;
if( fg ) first *= -;
//debug(first);
i ++;
//printf("i==%d ch==%c\n", i, str[i]); if( str[i]=='/' ) {
second = ;
p[len++] = make_pair(first, second);
break;
} else if( str[i]!='^' ) { second = ; }
else if( str[i]=='^' ) {
i ++;
while( isdigit(str[i]) ) {
second = second*+(str[i]-'');
i ++;
}
}
//debug(second);
p[len++] = make_pair(first, second);
cnt = max(cnt, second);
//debug(cnt);
//debug(len);
} m = ;
for(int i=; i<k; i++) {
if( str[i]=='/' ) {
i ++;
while( isdigit(str[i]) && i<k ) {
m = m* + (str[i]-'' );
i ++;
}
}
}
} void input(){
int cas = ;
while( cin >> str ) {
if( str[] == '.' ) break;
len = ;
k = str.length();
solve(k);
bool f = true;
for(int i=; i<=cnt+; i++) {
ll sum = ;
for(int j=; j<len; j++) {
//printf("first==%d second==%d\n", 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 ;
}