Ultraviolet A-1069 always an integer (analog)

Source: Internet
Author: User

Description

Combinatorics is a branch of mathematics chiefly concerned with counting discrete objects. For instance, how many ways can you pick two people out of a crowdNPeople? Into how many regions can you divide a circular disk by connectingNPoints on its boundary with one another? How many cubes are in a pyramid with square layers ranging from1 × 1N×NCubes?

Revoke questions like these have answers that can be forced CED to simple polynomials inN. The answer to the first question above isN(N-1)/2, or (N2-N)/2. The answer to the second is (N4-6N3 + 23N2-18N+ 24)/24. The answer to the third isN(N+ 1) (2N+ 1)/6, or (2N3 + 3N2 +N)/6. We write these polynomials in a standard form, as a polynomial with Integer Coefficients divided by a positive integer denominator.

These polynomials are answers to questions that can have integer answers only. But since they have fractional coefficients, they look as if they cocould produce non-integer results! Of course, evaluating these participating Polynomials on a positive integer always results in an integer. for other polynomials of similar form, this is not necessarily true. it can be hard to tell the two cases apart. so that, naturally, is your task.

Input

The input consists of multiple test cases, each on a separate line. Each test case is an expression in the form (P)/D, WherePIs a polynomial with Integer Coefficients andDIs a positive integer denominator.PIs a sum of terms of the formCNE, Where the CoefficientCAnd the exponentESatisfy the following conditions:

  1. EIs an integer satisfying 0E100. IfEIs 0, thenCNEIs expressedC. IfEIs 1, thenCNEIs expressedCN, UnlessCIs 1 or-1. In those instances,CNEIs expressedNOr-N.
  2. CIs an integer. IfCIs 1 or-1 andEIs not 0 or 1, thenCNEWill appearNEOr-NE.
  3. Only non-negativeCValues that are not part of the first term in the polynomial are preceded by +.
  4. Exponents in consecutive terms are strictly decreasing.
  5. CAndDFit in a 32-bit signed integer.

See the sample input for details.

Input is terminated by a line containing a single period.

Output

For each test case, print the case number (starting with 1). Then print'Always an integer'If the test case polynomial evaluates to an integer for every positive integerN. Print'Not always an integer'Otherwise. Print the output for separate test cases on separate lines. Your output shocould follow the same format as the sample output.

Sample Input

(n^2-n)/2 (2n^3+3n^2+n)/6 (-n^14-11n+1)/3 .

Sample output

Case 1: Always an integer Case 2: Always an integer Case 3: not always an integer, the idea of judging whether it gets an integer in all positive integers: the typical example of Liu lujia is too long. Let's draw a conclusion: P (1), P (2 ).. P (k + 1) is a multiple of D, and the N check is followed. The range of N is 1-k + 1.
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>typedef long long ll;using namespace std;const int maxn = 110;int c[maxn], e[maxn], p;int tot, len, l, tmp;char str[10001];int flag;ll pow_mod(ll a, ll p, ll n) {ll tmp = 1;while (p) {if (p & 1)tmp = (tmp * a) % n;p >>= 1;a = (a * a) % n;}return tmp;}void cal(int s, int a, int b) {int k, cs = 0, es = 0;for (k = a; k <= b; k++)if (str[k] == 'n')break;for (int i = a; i < k; i++)cs = cs * 10 + str[i] - '0';if (cs == 0)cs = s;else cs *= s;for (int i = k+2; i <= b; i++)es = es * 10 + str[i] - '0';if (k <= b && es == 0)es = 1;c[tot] = cs;e[tot++] = es;}int main() {int cas = 1;while (scanf("%s", str) && str[0] != '.') {len = strlen(str);l = tot = 0;if (str[1] == '-')l = 2;else l = 1;for (int i = l; i < len; i++) {if (str[i] == '+' || str[i] == '-' || str[i] == ')') {if (str[l-1] == '-')cal(-1, l, i-1);else cal(1, l, i-1);l = i+1;}if (str[i] == '/') {p = 0;for (int j = i+1; j < len; j++) p = p*10 + str[j] - '0';break;}}flag = 1;for (int i = 1; i < 200; i++) {tmp = 0;for (int j = 0; j < tot; j++)tmp = (tmp + (c[j]%p)*pow_mod(i, e[j], p)) % p;if (tmp) {flag = 0;break;}}if (flag) printf("Case %d: Always an integer\n", cas++);else printf("Case %d: Not always an integer\n", cas++);}return 0;}

 

Ultraviolet A-1069 always an integer (analog)

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.