Always an integer UVALive - 4119
题目很简单,就是求表达式(P/D)的结果是不是整数。其中P是一个整系数的多项式,D是一个正整数。
把1-k(最高次)+1都试一次就好了。结论可以总结归纳得到。(k取 0, 1, 2 .... 的情况推一次,可以推出结论)。
// Asimple
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <vector>
#include <string>
#include <cstring>
#include <stack>
#include <set>
#include <map>
#include <cmath>
#define INF 0x3f3f3f3f
#define mod 1000007
#define debug(a) cout<<#a<<" = "<<a<<endl
#define test() cout<<"============"<<endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = +;
ll n, m, T, len, cnt, num, ans, Max, k;
string str;
pair<ll, ll> p[maxn]; ll qpow(ll a, ll b, ll md) {
ll ans = ;
while( b ) {
if( b& ) ans = (ans*a)%md;
a = (a*a)%md;
b >>= ;
}
return ans;
} void solve(int k) {
cnt = ;
for(int i=; str[i]!=')'; ) {
ll first = ;
ll second = ;
bool fg = false;
while( str[i]!='n' && str[i]!=')' ) {
char ch = str[i];
//debug(ch);
if( ch==
'+') { }
else if( ch == '-' ) { fg = true; }
else first = first* + (ch-'');
i ++;
}
if( first == ) first = ;
if( fg ) first *= -;
//debug(first);
i ++;
//printf("i==%d ch==%c\n", i, str[i]); if( str[i]=='/' ) {
second = ;
p[len++] = make_pair(first, second);
break;
} else if( str[i]!='^' ) { second = ; }
else if( str[i]=='^' ) {
i ++;
while( isdigit(str[i]) ) {
second = second*+(str[i]-'');
i ++;
}
}
//debug(second);
p[len++] = make_pair(first, second);
cnt = max(cnt, second);
//debug(cnt);
//debug(len);
} m = ;
for(int i=; i<k; i++) {
if( str[i]=='/' ) {
i ++;
while( isdigit(str[i]) && i<k ) {
m = m* + (str[i]-'' );
i ++;
}
}
}
} void input(){
int cas = ;
while( cin >> str ) {
if( str[] == '.' ) break;
len = ;
k = str.length();
solve(k);
bool f = true;
for(int i=; i<=cnt+; i++) {
ll sum = ;
for(int j=; j<len; j++) {
//printf("first==%d second==%d\n", p[j].first, p[j].second);
sum += (p[j].first*qpow(i, p[j].second, m))%m;
sum %= m;
}
if( sum ) {
f = false;
break;
}
}
cout << "Case " << cas++ << ": ";
if( f ) cout << "Always an integer" << endl;
else cout << "Not always an integer" << endl;
}
} int main() {
input();
return ;
}
Always an integer UVALive - 4119的更多相关文章
- UVALive 4119 Always an integer (差分数列,模拟)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Always an integer Time Limit:3000MS M ...
- uvalive 4119 Always an Interger
差分数列+字符串处理 题意:是让你判断一个整系数多项式的值是否一直都能被一个所给的正整数所整除. 通过对差分数列的不断求导,我们可以发现,对于任意多项式P,我们只需要判断n从1到k+1是否满足就行了, ...
- LA 4119 - Always an integer
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- LA 4119 (差分数列 多项式) Always an integer
题意: 给出一个形如(P)/D的多项式,其中P是n的整系数多项式,D为整数. 问是否对于所有的正整数n,该多项式的值都是整数. 分析: 可以用数学归纳法证明,若P(n)是k次多项式,则P(n+1) - ...
- LA 4119 Always an integer (数论+模拟)
ACM-ICPC Live Archive 一道模拟题,题意是问一个给出的多项式代入正整数得到的值是否总是整数. 这题是一道数论题,其实对于这个式子,我们只要计算1~最高次项是否都满足即可. 做的时候 ...
- UVALive 4031 Integer Transmission(贪心 + DP)
分析:求出最大值和最小值比较简单,使用贪心法,求最小值的时候我们让所有的0尽可能的向后延迟就可以了,求最大值则相反. 关键在于求出可以组合出的数字个数. 这就是组合数学版的dp了,我们让dp[i][j ...
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- 【暑假】[实用数据结构]UVAlive 3135 Argus
UVAlive 3135 Argus Argus Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %l ...
随机推荐
- [硬件]Robot运动控制
思考问题:机器人运动控制如何与图形界面交互? 不得不说,先锋机器人的软件做的真不怎么样.图形界面交互用户体验很差. 现在我遇到一个很现实的问题:SLAM需要采集激光数据和机器人的位姿,同时我还要再这个 ...
- 为什么char *name="it",printf("%s",name) 能够输出字符串?
“it”里面是3个字符 “i”“t”“/0”,%s会打印指针指向的字符就是“i”,知道遇到“/0”停止,所以打印出来是“it”
- ORM模板层
1.模板语言之变量 def index(request): name='lqz' age=18 ll=['name','age'] dic={'name':name,'age':age} class ...
- Django 框架 django的请求生命周期
概述 首先我们知道HTTP请求及服务端响应中传输的所有数据都是字符串,同时http请求是无状态的,可以通过session和cookie来辅助. 浏览器通过ip和端口及路由方式访问服务端. 在Djang ...
- MySQL主从同步问题
1,The replication receiver thread cannot start because the master has GTID_MODE = OFF and this serve ...
- Python_summary
Q: python中出现IndentationError:unindent does not match any outer indentation levelA:复制代码的时候容易出现缩进错误,虽然 ...
- es6proxy
Proxy 支持的拦截操作一览. 对于可以设置.但没有设置拦截的操作,则直接落在目标对象上,按照原先的方式产生结果. (1)get(target, propKey, receiver) 拦截对象属性的 ...
- Bootstrap-全局CSS样式-图片样式
Bootstrap第二部分:全局CSS样式-图片样式.img-rounded 圆角图片.img-circle 圆形图片.img-thumbnail 缩略图片.img-responsive响应 ...
- jquery $.ajax $.get $.post的区别?
$.ajax 是 jQuery 底层 AJAX 实现,$.ajax是一种通用的底层封装,$.ajax()请求数据之后,则需要使用回调函数,有beforeSend.error.dataFilter.su ...
- Mysql 修改字段长度、修改列名、新增列、修改自增主键起始值
alter table 表名 modify column 字段名 类型; 例如 数据库中user表 name字段是varchar(30) 可以用 ) ; --修改字段长度 )--修改表列名 ); -- ...