• LA 4119 Always an integer (数论+模拟)


    ACM-ICPC Live Archive

      一道模拟题,题意是问一个给出的多项式代入正整数得到的值是否总是整数。

      这题是一道数论题,其实对于这个式子,我们只要计算1~最高次项是否都满足即可。

      做的时候,模拟出了点小错误,一直wa。幸亏最后还是能找到错误的位置。

    代码如下:

      1 #include <iostream>
      2 #include <algorithm>
      3 #include <cstdio>
      4 #include <cstring>
      5 #include <cctype>
      6 
      7 using namespace std;
      8 
      9 const int N = 11111;
     10 typedef long long LL;
     11 char str[N];
     12 
     13 LL powmod(LL a, LL p, LL m) {
     14     LL ret = 1;
     15     while (p > 0) {
     16         if (p & 1) ret *= a, ret %= m;
     17         a *= a, a %= m;
     18         p >>= 1;
     19     }
     20     return ret;
     21 }
     22 
     23 char *fuckbrace(char *s) {
     24     char *p = s;
     25     if (*p == '(') {
     26         while (*s && *s != ')') s++;
     27         *s = 0;
     28         p++;
     29     }
     30     return p;
     31 }
     32 
     33 void scan(char *s, LL &n) {
     34     while (*s && !isdigit(*s)) s++;
     35     n = 0;
     36     while (*s && isdigit(*s)) n = n * 10 + *s - '0', s++;
     37 }
     38 
     39 LL gettop(char *p) {
     40     char *i = strstr(p, "^");
     41     LL ret = 1;
     42     if (i) scan(i, ret);
     43     return ret;
     44 }
     45 
     46 LL cal(char *s, LL x, LL m) {
     47     LL ret = 0, tmp = 1, t, p;
     48     while (*s) {
     49         if (isdigit(*s)) {
     50             scan(s, t);
     51             tmp *= t;
     52             tmp %= m;
     53             while (isdigit(*s)) s++;
     54             //cout << *s << endl;
     55             if (*s == 'n') {
     56                 LL temp = tmp;
     57                 tmp *= x;
     58                 tmp %= m;
     59                 if (*(s + 1) == '^') {
     60                     scan(s + 2, p);
     61                     if (p == 0) tmp = temp;
     62                     tmp *= powmod(x, p - 1, m);
     63                     tmp %= m;
     64                     s += 2;
     65                     while (isdigit(*s)) s++;
     66                 } else s++;
     67             }
     68             ret += tmp;
     69             ret %= m;
     70             tmp = 1;
     71         } else if (*s == '-') {
     72             tmp *= -1;
     73             s++;
     74         } else if (*s == '+') s++;
     75         else if (*s == 'n') {
     76             //cout << "is n??" << endl;
     77             LL temp = tmp;
     78             tmp *= x;
     79             tmp %= m;
     80             if (*(s + 1) == '^') {
     81                 scan(s + 2, p);
     82                 if (p == 0) tmp = temp;
     83                 //cout << x << ' ' << p << ' ' << m << ' ' << powmod(x, p - 1, m) << endl;
     84                 tmp *= powmod(x, p - 1, m);
     85                 tmp %= m;
     86                 s += 2;
     87                 while (isdigit(*s)) s++;
     88             } else s++;
     89             //cout << ret << ' ' << tmp << endl;
     90             ret += tmp;
     91             ret %= m;
     92             tmp = 1;
     93         } else {
     94             puts("WTF?...");
     95             cout << s << endl;
     96             while (1) ;
     97         }
     98     }
     99     return ret;
    100 }
    101 
    102 bool check(char *s, LL a, LL b, LL m) {
    103     for (LL i = a; i <= b; i++) {
    104         if (cal(s, i, m)) return false;
    105         //cout << s << ' ' << i << ' ' << m << ' ' << cal(s, (LL) i, m) << endl;
    106     }
    107     return true;
    108 }
    109 
    110 int main() {
    111     int cas = 1;
    112     //freopen("in", "r", stdin);
    113     while (cin >> str && strcmp(str, ".")) {
    114         LL D;
    115         char *p = str;
    116         while (*p && *p != '/') p++;
    117         if (*p) {
    118             *(p++) = 0;
    119             sscanf(p, "%lld", &D);
    120         } else {
    121             puts("are u kidding?= =");
    122             while (1) ;
    123         }
    124         if (D > 0x7fffffff) {
    125             puts("fuck you");
    126             while (1) ;
    127         }
    128         p = fuckbrace(str);
    129         //cout << p << ' ' << D << endl;
    130         LL top = gettop(p);
    131         //cout << top << endl;
    132         if (check(p, 1, top << 1, D)) printf("Case %d: Always an integer
    ", cas++);
    133         else printf("Case %d: Not always an integer
    ", cas++);
    134     }
    135     return 0;
    136 }
    View Code

    ——written by Lyon

  • 相关阅读:
    get、put、post、delete含义与区别
    中奖数据表设计方案
    mysql递归
    java Ajax跨域请求COOKIE无法带上的解决办法
    tomcat 下配置ajax 跨域 tomcat font face 跨域 java跨域
    JAVA入门教程
    解决Navicat 出错:1130-host . is not allowed to connect to this MySql server,MySQL
    Hbuilder开发app时生成ios要的mobileprovision和p12文件步骤.
    js 获取浏览器高度和宽度值(多浏览器)
    Windows netstat 查看端口、进程占用 查看进程路径
  • 原文地址:https://www.cnblogs.com/LyonLys/p/LA_4119_Lyon.html
Copyright © 2020-2023  润新知