uva 1069 - Always an integer(数学+暴力)

题目链接:uva 1069 - Always an integer

题目大意:给出一个多次多项式,问说是否对于任意正整数n来说结构均为整数。

解题思路:首先处理出字符串,然后枚举n从1到k+1判断即可,k为多项式中出现过的最大幂数指。

P为多项式,d为除数,k为多项式中最大的次数

  • 当k=0时,P中不存在n变量,所以直接计算判断即可
  • 当k=1时,P是一次多项式,那么P(n+1)?P(n)=a,a为常数,也就是说P(i)为等差数列,如果首项P(1)和公差P(2)-P(1)为d的倍数即可,等价与判断P(1)和P(2)是否为d的倍数。
  • 当k=2是,P是一个k次的多项式,那么S(n)=P(n+1)?P(n)=2an+a+b,也就是说首先是S(1)是d的倍数,并且差数列S(n)也要是d的倍数才可以,S(n)其实就是k=1的情况,需要使得S(n)的每项均为d的倍数,同样是是首项和公差必须是d的倍数,a0=S(1)=P(2)?P(1),公差S(2)?S(1)=P(3)?P(1),等价与判断P(1),P(2),P(3)是否为d的倍数。
#include <cstdio>
#include <cstring>

typedef long long ll;
const int N = 105;
const int M = N * N;

ll c[N], d;
char str[M];

inline bool isDight (char ch) {
    return ch >= ‘0‘ && ch <= ‘9‘;
}

void init () {

    memset(c, 0, sizeof(c));

    ll k = 0, a = 0, key = 0, sign = 1;
    d = 0;

    int len  = strlen(str);
    for (int i = 0; i <= len; i++) {
        if (isDight(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 power(ll x, int n, ll MOD) {
    ll ans = 1;
    while (n) {
        if (n&1)
            ans = (ans * x) % MOD;
        x = (x * x) % MOD;
        n /= 2;
    }
    return ans;
}

bool judge () {
    int n = N-1;
    while (c[n] == 0)
        n--;

    /*
    for (int i = 0; i <= n; i++) {
        if (c[i])
            printf("%d %lld\n", i, c[i]);
    }
    */

    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] * power(i, j, d)) % d;
            ans = (ans + d) % d;
        }

        if (ans)
            return false;
    }

    return true;
}

int main () {
    int cas = 1;
    while (scanf("%s", str) == 1 && 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(数学+暴力)

时间: 2024-11-10 01:33:19

uva 1069 - Always an integer(数学+暴力)的相关文章

Uva 1069 Always an Integer ( 数学 )

Uva 1069 Always an Integer ( 数学 ) #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

UVA 1069 - Always an integer(数论)

1069 - Always an integer 题意:给定一个多项式,推断是否总是整数 思路:LRJ大白上的例题,上面给出了证明,仅仅要1到k + 1(k为最高次数)带入方程都是整数,那么整个方程就是整数,处理出字符串后,然后过程用高速幂计算,推断最后答案是否为0,看是否全都满足是整数. 代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; char s

UVA - 1069 Always an integer (模拟)

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 crowd ofn people? Into how many regions can you divide a circular disk by connectingn

uva 639 Don&#39;t Get Rooked (暴力回溯 )

uva 639 Don't Get Rooked In chess, the rook is a piece that can move any number of squares vertically or horizontally. In this problem we will consider small chess boards (at most 44) that can also contain walls through which rooks cannot move. The g

uva live 4123 Glenbow Museum 数学递推

// uva live 4123 Glenbow Museum 数学递推 // // 题目大意: // // 对于一个边平行于坐标轴的多边形,我们可以用一个序列来描述,R和O,R表示 // 该顶点的角度为90度,O表示该定点的角度为270.给定序列的长度.问能由这些RO序 // 列组成的星型多边形(内部存在一个点可以看到所有的节点的多边形)的方法数有多少. // // 解题思路: // // 顶点数一定是序列的长度.n小于4无法组成多边形.n为奇数的时候也不行.设R的个数有 // x个,O的个数

uva 11490 - Just Another Problem(数学)

题目链接:uva 11490 - Just Another Problem 题目大意:有n个士兵,要排列成一个方阵,要求方阵尽量大,于是在方正的中间会空出两个正方形的区域,空出来的局域要求厚度相同,即正方形的四条边向相应方向均再有x行或者列. 解题思路:根据题意可以知道x(6x+7r)=n,x为厚度,r为正方形的边长.接着枚举x,x是n的因子. #include <cstdio> #include <cstring> #include <cmath> typedef l

UVA 152-Tree&#39;s a Crowd(暴力求解三维坐标求最短距离)

Tree's a Crowd Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description  Tree's a Crowd  Dr William Larch, noted plant psychologist and inventor of the phrase ``Think like a tree--Think Fig'' has invented a new

UVA 12723 Dudu, the Possum --数学期望

题意不说了,概率和期望值要分开处理,可以先算出到达每层的概率,然后再乘以每层的期望,每层的期望是固定的.也可以从后往前直接推期望. #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 100007 double ex[516]; double dp[

Uva 1315 - Creaz tea party ( 数学 + 规律 )

Uva 1315 - Creaz tea party (  数学 + 规律 ) 题意: 一个环,围在一个坐了N个人.每次操纵可以交换相邻的两个人的位置.求最少需要交换几次,可以变为逆序. 这里的逆序指的是原来在A左边的人在A的右边,在A右边的在A的左边. 分析: 如果是线性的,,,果断类似冒牌排序(n)(n-1)/2 但是这里是环,推了推但是推不出结果..结论是将环分为两段线性的序列,线性的满足上面的公式. 如:     1 2 3 4 5  线性:  5 4 3 2 1  ( 10次 ) 环状