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.小奔的方案 当然本次比赛肯定难度不会仅限于此啦!后续还会--
随机推荐
- springMVC--动态验证码实现
在网站开发过程中我们一般都会为了防止用户连续提交都会提供验证码的功能,简单来说就是生成一个动态图片,在图片中保存一些校验信息,将校验信息放到session中和用户提交的验证码信息进行对比,如果出现错误 ...
- day18 12.丢失更新介绍与悲观锁
共享锁在一条记录上是可以加多个的,共享嘛.排它锁的意思是指这条记录上如果有任何其他的锁我排它锁是加不上的,有了排它锁其他锁也是加不上的,唯一的.比如说现在我的记录上没锁,加了排它锁其他人使用不了,我这 ...
- MySQL常见数据库引擎及对比
一:MySQL存储引擎简介 MySQL有多种存储引擎,每种存储引擎有各自的优缺点,大家可以择优选择使用:MyISAM.InnoDB.MERGE.MEMORY(HEAP).BDB(BerkeleyDB) ...
- http响应头信息
HTTP 响应头信息 HTTP请求头提供了关于请求,响应或者其他的发送实体的信息. 在本章节中我们将具体来介绍HTTP响应头信息. 应答头 说明 Allow 服务器支持哪些请求方法(如GET.POST ...
- Django项目:CRM(客户关系管理系统)--11--04PerfectCRM实现King_admin注册功能03
#base_admin.py #Django admin 注册功能的形式 # sites = { # 'crm':{ # 'customers':CustomerAdmin, # 'customerf ...
- loadrunner11录制为空的解决办法(win7+chrome最新版本)
参考:https://www.cnblogs.com/zhang-zhi/archive/2018/09/10/9622605.html loadrunner11在win7中,使用chrome浏览器的 ...
- 安装 cx_Oracle
1.下载 oracle client instant 和 sdk, 全部解压到 /opt/instantclient_11_2/ http://www.oracle.com/technetwor ...
- css3烟花效果
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- DMSkin https://github.com/944095635/DMSkin
与源码无关内容 1.如果你有XAML相关的外包需求,可以通过QQ或微信与我取得联系.(QQ:"944095635" 微信号:"qq944095635") 2.我 ...
- Java加密AES算法及spring中应用
开门见山直接贴上代码 .AESUtil加密解密工具类 import java.security.Key; import java.security.SecureRandom; import java. ...