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 ...
随机推荐
- TCP接收缓存大小的手动调整
给出了几个可调节的参数,它们可以帮助您提高 Linux TCP/IP 栈的性能. 表 1. TCP/IP 栈性能使用的可调节内核参数 可调节的参数 默认值 选项说明 /proc/sys/net/cor ...
- [py]py里的isinstance判断实例来源(含父类)
Isinstance() 函数来判断一个对象是否是一个已知的类型,类似 type(). isinstance() 与 type() 区别: type() 不会认为子类是一种父类类型,不考虑继承关系. ...
- leetcode 300最长上升子序列
用递归DFS遍历所有组合肯定积分会超时,原因是有很多重复的操作,可以想象每次回溯后肯定会有重复操作.所以改用动态规划.建立一个vector<int>memo,初始化为1,memo[i]表示 ...
- svn回复历史版本的操作方法
svn恢复历史版本的操作方法svn update 保证最新svn log login.php 获得版本号svn diff -r 11026:11027 login.php 发现11027出问题svn ...
- vue+element-ui中的表单验证(电话等等)
1. 2. 3. ============================================================上代码============================ ...
- cocos2d JS 创建实现换行功能的聊天文本 testLable
-- 创建实现换行功能的文本 function LoadLabelHeroIntroduce(objLabelContent) //传入文本 ---------------------------- ...
- hibernate.validator.constraints.NotEmpty校验请求参数报错java.lang.NoClassDefFoundError: javax/el/PropertyNotFoundException
spring maven项目,使用hibernate validator 注解形式校验客户端的请求参数. hibernate-validator版本:5.0.2.Final validation-ap ...
- c3p0:Connections could not be acquired from the underlying database!解决方案
在利用ssh框架做网站的时候遇到了一个比较棘手的问题,一直连接不上数据库,问题描述如下: 各种百度然后说的最多的解决方案是: 1,驱动配置有误:2,数据库连接地址有误:3,密码或帐号有误: 4,数据库 ...
- spring自带的定时任务功能@EnableScheduling
1 demo package com.test.domi.config; import org.springframework.beans.factory.annotation.Configurabl ...
- node.js中express模块创建服务器和http模块客户端发请求
首先下载express模块,命令行输入 npm install express 1.node.js中express模块创建服务端 在js代码同文件位置新建一个文件夹(www_root),里面存放网页文 ...