ACM-ICPC Live Archive

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

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

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

代码如下:

 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cctype> using namespace std; const int N = ;
typedef long long LL;
char str[N]; LL powmod(LL a, LL p, LL m) {
LL ret = ;
while (p > ) {
if (p & ) ret *= a, ret %= m;
a *= a, a %= m;
p >>= ;
}
return ret;
} char *fuckbrace(char *s) {
char *p = s;
if (*p == '(') {
while (*s && *s != ')') s++;
*s = ;
p++;
}
return p;
} void scan(char *s, LL &n) {
while (*s && !isdigit(*s)) s++;
n = ;
while (*s && isdigit(*s)) n = n * + *s - '', s++;
} LL gettop(char *p) {
char *i = strstr(p, "^");
LL ret = ;
if (i) scan(i, ret);
return ret;
} LL cal(char *s, LL x, LL m) {
LL ret = , tmp = , t, p;
while (*s) {
if (isdigit(*s)) {
scan(s, t);
tmp *= t;
tmp %= m;
while (isdigit(*s)) s++;
//cout << *s << endl;
if (*s == 'n') {
LL temp = tmp;
tmp *= x;
tmp %= m;
if (*(s + ) == '^') {
scan(s + , p);
if (p == ) tmp = temp;
tmp *= powmod(x, p - , m);
tmp %= m;
s += ;
while (isdigit(*s)) s++;
} else s++;
}
ret += tmp;
ret %= m;
tmp = ;
} else if (*s == '-') {
tmp *= -;
s++;
} else if (*s == '+') s++;
else if (*s == 'n') {
//cout << "is n??" << endl;
LL temp = tmp;
tmp *= x;
tmp %= m;
if (*(s + ) == '^') {
scan(s + , p);
if (p == ) tmp = temp;
//cout << x << ' ' << p << ' ' << m << ' ' << powmod(x, p - 1, m) << endl;
tmp *= powmod(x, p - , m);
tmp %= m;
s += ;
while (isdigit(*s)) s++;
} else s++;
//cout << ret << ' ' << tmp << endl;
ret += tmp;
ret %= m;
tmp = ;
} else {
puts("WTF?...");
cout << s << endl;
while () ;
}
}
return ret;
} bool check(char *s, LL a, LL b, LL m) {
for (LL i = a; i <= b; i++) {
if (cal(s, i, m)) return false;
//cout << s << ' ' << i << ' ' << m << ' ' << cal(s, (LL) i, m) << endl;
}
return true;
} int main() {
int cas = ;
//freopen("in", "r", stdin);
while (cin >> str && strcmp(str, ".")) {
LL D;
char *p = str;
while (*p && *p != '/') p++;
if (*p) {
*(p++) = ;
sscanf(p, "%lld", &D);
} else {
puts("are u kidding?= =");
while () ;
}
if (D > 0x7fffffff) {
puts("fuck you");
while () ;
}
p = fuckbrace(str);
//cout << p << ' ' << D << endl;
LL top = gettop(p);
//cout << top << endl;
if (check(p, , top << , D)) printf("Case %d: Always an integer\n", cas++);
else printf("Case %d: Not always an integer\n", cas++);
}
return ;
}

——written by Lyon

LA 4119 Always an integer (数论+模拟)的更多相关文章

  1. LA 4119 - Always an integer

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  2. UVALive 4119 Always an integer (差分数列,模拟)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Always an integer Time Limit:3000MS     M ...

  3. LA 4119 (差分数列 多项式) Always an integer

    题意: 给出一个形如(P)/D的多项式,其中P是n的整系数多项式,D为整数. 问是否对于所有的正整数n,该多项式的值都是整数. 分析: 可以用数学归纳法证明,若P(n)是k次多项式,则P(n+1) - ...

  4. 【BZOJ-4522】密钥破解 数论 + 模拟 ( Pollard_Rho分解 + Exgcd求逆元 + 快速幂 + 快速乘)

    4522: [Cqoi2016]密钥破解 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 290  Solved: 148[Submit][Status ...

  5. UVA 1069 - Always an integer(数论)

    1069 - Always an integer 题意:给定一个多项式,推断是否总是整数 思路:LRJ大白上的例题,上面给出了证明,仅仅要1到k + 1(k为最高次数)带入方程都是整数,那么整个方程就 ...

  6. 2018中国大学生程序设计竞赛 - 网络选拔赛 hdu Find Integer 数论

    Find Integer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  7. Leetcode 7 Reverse Integer 数论

    题意:将整数倒置,该题简单但是需要注意数据的范围,难得的好题. 如果出现1000000003或者-2000000003,倒置后的数超过int的范围,因此返回0,出现这种情况可以使用long long, ...

  8. Always an Integer 数论和字符串处理

    题意:判断一个整系数多项式除以一个常数结果是否一定是一个整数 大白p123例题.可以随机代入一些n的值,判断一下.不过只要代入1到k+1(k为多项式最高项的次数)即可.通过数学归纳法证明,先讨论k为0 ...

  9. 【CYH-02】noip2018数论模拟赛:赛后题解

    1.小奔的矩阵 2.大奔的方案 3.小奔与不等四边形 4.小奔的方案 当然本次比赛肯定难度不会仅限于此啦!后续还会--

随机推荐

  1. birt运行环境

    1.下载 http://pan.baidu.com/s/1nvhz5wt 2.解压birt-runtime-4.6.0-20160607.zip 将WebViewerExample更名为birt,复制 ...

  2. BZOJ1455罗马游戏

    左偏树裸题. 题面描述让人意识到了平面几何的重要性. //Achen #include<algorithm> #include<iostream> #include<cs ...

  3. Android中使用ormlite实现持久化--HelloOrmLite

    Android中内置了sqlite,但是常用的开发语言java是面向对象的,而数据库是关系型的,二者之间的转化每次都很麻烦(主要是我对sql语言不熟悉).而Java Web开发中有很多orm框架,但是 ...

  4. 安装 cx_Oracle

    1.下载 oracle client instant  和  sdk,  全部解压到 /opt/instantclient_11_2/ http://www.oracle.com/technetwor ...

  5. OpenLayers添加和删除控件

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  6. 介绍vue项目中的axios请求(get和post)

    一.先安装axios依赖,还有qs依赖 npm install axios --save npm install qs --save qs依赖包用post请求需要用到的 插入一个知识点: npm in ...

  7. c标签 if else c标签 总结

    [b]STL标签用法 关键字:JSTL标签.<c:choose>.<c:forEach>.<c:forTokens>.<c:if>.<c:impo ...

  8. GNU的__builtin_popcount函数

    用来计算32位的unsigned int中的1的个数, 其内部实现是根据查表法来计算的.

  9. php-imagick扩展

    介绍 magick 是用 ImageMagic API 来创建和修改图像的PHP官方扩展.ImageMagick® 是用来创建,编辑,合并位图图像的一套组件. 它能够用于读取,转换,写入多种不同格式的 ...

  10. LeedCode OJ --- Binary Tree Inorder Traversal

    点击打开题目链接 今天只是写了递归的版本,因为还没想好怎么用迭代来实现,可以写的过程中,有一点是有疑问的,虽然我的代码可以AC. 问题是:主调函数是可以使用子函数中返回的在子函数中定义的vector. ...