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. Leetcode581.Shortest Unsorted Continuous Subarray最短无序连续子数组

    给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 示例 1: 输入: [2, 6, 4, 8, 1 ...

  2. phpBOM头(字符&#65279;)出现的原因以及解决方法_PHP程序员博客|高蒙个人博客

    今天在项目中发现,客户端在使用ajax得到返回值时,无法匹配字符串.总是报错,打开页面接口发现,页面的头部出现了的字符(BOM头),找到问题了,那么直接用代码清除掉即可. php隐形字符 // 如 ...

  3. mybatis的第一个程序

    程序结构图: 表结构: 创表sql: CREATE TABLE `users` (   `id` int(11) NOT NULL AUTO_INCREMENT,   `username` varch ...

  4. cf round 482D Kuro and GCD and XOR and SUM

    题意: 开始有个空集合,现在有两种操作: $(1,x)$:给集合加一个数$x$,$x \leq 10^5$; $(2,x,k,s)$:在集合中找一个$a$,满足$a \leq s-x$,而且$k|gc ...

  5. Python3 中 configparser 使用注意事项

    在使用configparser时候应注意: ①配置文件(ini文件)的存放位置:配置文件和调用文件放在同一个文件包下面. 使用read()函数读取并解析配置文件时,直接写配置文件(ini文件)的文件名 ...

  6. day1---转自金角大王

    金角大王等待唐僧的日子 Yesterday, when I was young, There were so many songs that waited to be sung. 博客园 首页 新随笔 ...

  7. Unity3D研究院之为什么Inspector视图中脚本前面的勾选框没了

    我一个同事刚问我为啥有时候脚本的勾选项没有了?有时候不想让某条脚本执行,可以直接在编辑器中点掉勾选按钮即可.如下图所示 以前我也遇到过这个问题,但是一直都没怎么注意,因为一般情况下也用不到.今天刚好有 ...

  8. Springboot项目下mybatis报错:Invalid bound statement (not found)

    mybatis报错:Invalid bound statement (not found)的原因很多,但是正如报错提示一样,找不到xml中的sql语句,报错的情况分为三种: 第一种:语法错误 Java ...

  9. 【水滴石穿】ReactNativeDemo

    这个博主他的功底算是特别棒的了,能够把一些基础的例子,通过精巧的方式布局在一个小的demo里面 很值得我学习 放上博主的链接:https://github.com/jianiuqi/ReactNati ...

  10. 无线传感网络协议——Smart Mesh IP

    前言: SmartMesh IP 专为实现 IP 兼容性而设计,并基于 6LoWPAN 和 802.15.4e 标准.SmartMesh IP 产品线实现了网络适应性.可靠性和可扩展性水平,并拥有高级 ...