LA 4119 Always an integer (数论+模拟)
一道模拟题,题意是问一个给出的多项式代入正整数得到的值是否总是整数。
这题是一道数论题,其实对于这个式子,我们只要计算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 (数论+模拟)的更多相关文章
- LA 4119 - Always an integer
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVALive 4119 Always an integer (差分数列,模拟)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Always an integer Time Limit:3000MS M ...
- LA 4119 (差分数列 多项式) Always an integer
题意: 给出一个形如(P)/D的多项式,其中P是n的整系数多项式,D为整数. 问是否对于所有的正整数n,该多项式的值都是整数. 分析: 可以用数学归纳法证明,若P(n)是k次多项式,则P(n+1) - ...
- 【BZOJ-4522】密钥破解 数论 + 模拟 ( Pollard_Rho分解 + Exgcd求逆元 + 快速幂 + 快速乘)
4522: [Cqoi2016]密钥破解 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 290 Solved: 148[Submit][Status ...
- UVA 1069 - Always an integer(数论)
1069 - Always an integer 题意:给定一个多项式,推断是否总是整数 思路:LRJ大白上的例题,上面给出了证明,仅仅要1到k + 1(k为最高次数)带入方程都是整数,那么整个方程就 ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 hdu Find Integer 数论
Find Integer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- Leetcode 7 Reverse Integer 数论
题意:将整数倒置,该题简单但是需要注意数据的范围,难得的好题. 如果出现1000000003或者-2000000003,倒置后的数超过int的范围,因此返回0,出现这种情况可以使用long long, ...
- Always an Integer 数论和字符串处理
题意:判断一个整系数多项式除以一个常数结果是否一定是一个整数 大白p123例题.可以随机代入一些n的值,判断一下.不过只要代入1到k+1(k为多项式最高项的次数)即可.通过数学归纳法证明,先讨论k为0 ...
- 【CYH-02】noip2018数论模拟赛:赛后题解
1.小奔的矩阵 2.大奔的方案 3.小奔与不等四边形 4.小奔的方案 当然本次比赛肯定难度不会仅限于此啦!后续还会--
随机推荐
- mysql查询 包含某个字符的记录
从excel导入数据库的时候,发现poi自动把电话号码转换为科学计数法了 所以要把带e的筛选出来 SELECT * FROM t_customer WHERE phone like '%E%'; 然后 ...
- php pdo操作数据库的方法
PDO 安装 你可以通过 PHP 的 phpinfo() 函数来查看是否安装了PDO扩展. 1.在 Unix /linux系统上安装 PDO 在Unix上或Linux上你需要添加以下扩展: exten ...
- windows无法启动MySQL服务报错1067的解决方法是怎样?
方法一: 1.打开my.ini文件,找到default-storage-engine=InnoDB这一行,把它改成default-storage-engine=MyISAM.2.删除在MySQL安装目 ...
- Django与HTML业务基本结合--基本的用户名密码提交方法1
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 关于rss的内容(转载)
转载自: https://blog.csdn.net/zhao1949/article/details/52806123 (本文对读者有帮助的话请移步支持原作者) 内容记录: 在C++技术网开通了RS ...
- 对List<Map<String, Object>>集合排序
private void mySort(List<Map<String, Object>> list) { //list为待排序的集合,按SEQ字段排序 Comparator& ...
- LintCode 斐波纳契数列
查找斐波纳契数列中第 N 个数. 所谓的斐波纳契数列是指: 前2个数是 0 和 1 . 第 i 个数是第 i-1 个数和第i-2 个数的和. 斐波纳契数列的前10个数字是: 0, 1, 1, 2, 3 ...
- XML配置里的Bean自动装配与Bean之间的关系
需要在<bean>的autowire属性里指定自动装配的模式 byType(根据类型自动装配) byName(根据名称自动装配) constructor(通过构造器自动装配) 名字须与属性 ...
- SQLServer —— 数据类型的转换
一.使用convert函数实现强制转换 例如我们现在有如下一张学员成绩表: 现在想查询学号等于100003的学员总成绩,并按照要求打印出来,我们可以这样实现: 结果报错,因为最后一句字符串不能和数值相 ...
- org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of com.trs.om.bean.User.retryCount
六月 29, 2019 5:42:45 下午 org.apache.catalina.core.AprLifecycleListener init信息: The APR based Apache To ...