uvalive 4119 Always an Interger
差分数列+字符串处理
题意:是让你判断一个整系数多项式的值是否一直都能被一个所给的正整数所整除。
通过对差分数列的不断求导,我们可以发现,对于任意多项式P,我们只需要判断n从1到k+1是否满足就行了,其中,k为多项式P中的最高次数。
可以先了解一下差分数列。
#include <iostream>
#include <cstdio>
#include<cstdlib>
#include<cctype>
#include<string>
#include<vector>
#include<cassert>
using namespace std;
struct Polynomial
{
vector<int >a,p;
void parse_polynomial(string str)
{
int len=str.size(),i=;
while(i<len)
{
int sign=;
if(str[i]=='+')
i++;
if(str[i]=='-')
{
i++;
sign=-;
}
int v=;
while(i<len&&isdigit(str[i]))
v=v*+str[i++]-'';
if(i==len)
{
a.push_back(v);
p.push_back();
}
else
{
assert(str[i] == 'n');
if(v==)
v=;
v*=sign;
if(str[++i]=='^')
{
a.push_back(v);
v=;
i++;
while(i<len&&isdigit(str[i]))
v=v*+str[i++]-'';
p.push_back(v);
}
else
{
a.push_back(v);
p.push_back();
}
}
}
}
int mod(int x,int MOD)
{
int n=a.size();
int ans=;
for(int i=; i<n; i++)
{
int m=a[i];
for(int j=; j<p[i]; j++)
{
m=(long long)m*x%MOD;
}
ans=((long long)ans + m) % MOD;
}
return ans;
}
}; bool check(string str)
{
int p=str.find('/');
Polynomial poly;
poly.parse_polynomial(str.substr(, p-));
int D = atoi(str.substr(p+).c_str());
for(int i=; i<=poly.p[]+;i++)
{
if(poly.mod(i,D))
return false;
}
return true;
} int main()
{
int cas=;
string str;
while(cin>>str)
{
if(str[]=='.')
break;
printf("Case %d: ", cas++);
if(check(str))
printf("Always an integer\n");
else
printf("Not always an integer\n");
}
return ;
}
uvalive 4119 Always an Interger的更多相关文章
- UVALive 4119 Always an integer (差分数列,模拟)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Always an integer Time Limit:3000MS M ...
- Always an integer UVALive - 4119
题目很简单,就是求表达式(P/D)的结果是不是整数.其中P是一个整系数的多项式,D是一个正整数. 把1-k(最高次)+1都试一次就好了.结论可以总结归纳得到.(k取 0, 1, 2 .... 的情况推 ...
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- Interger 与 int
int是java提供的8种原始数据类型之一.Java为每个原始类型提供了封装类,Integer是java为int提供的封装类.int的默认值为0,而Integer的默认值为null,即Integer可 ...
- 思维 UVALive 3708 Graveyard
题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...
- UVALive 6145 Version Controlled IDE(可持久化treap、rope)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALive 6508 Permutation Graphs
Permutation Graphs Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
随机推荐
- pl/sql developer连接远程数据库
本地不安装oracle client程序,直接使用pl/sql developer连接远程数据库 考虑到机子本身资源有限,一个client会占用很多资源,尝试使用不安装客户端的方式进行远程连接. 需要 ...
- 下一代Jquery模板-----JsRender
在ASP.NET MVC利用PagedList分页(二)PagedList+Ajax+JsRender中提到了JsRender.JsRedner和JsViews(JsViews是再JsRender基础 ...
- android 使用系统照相程序照相并存储、显示在界面上
大部分业务可以通过调用系统的相机程序来拍照. 界面如下: <?xml version="1.0" encoding="utf-8"?> <Li ...
- 关于static继承的问题
c++primer 15.2.7节关于static继承的意思是,父类和子类共享static函数或者static成员变量,并且子类要访问还要受它们的权限限制,下面是看到的另一个例子 class Base ...
- Qt: qobject_cast<QPushButton*>(sender()) 简化信号与槽的编写
当你觉得写代码是一件重复性极高的工作时,这时你就应该考虑换个方式来实现了. 提高代码效率,减少代码量. 代码片: void Widget::onClicked() { QPushButton* but ...
- 屏蔽QQ聊天对话框中的广告
原文地址: 怎么在QQ聊天对话框中屏蔽广告_百度经验 http://jingyan.baidu.com/article/48a42057ca12c1a924250402.html QQ已经成为 ...
- SQL Server 2012 连接到数据库引擎
第 1 课:连接到数据库引擎 https://msdn.microsoft.com/zh-cn/library/ms345332(v=sql.110).aspx 本课将介绍主要的工具以及如何连接并 ...
- JS动态特性
var arr=[]; arr['js']='jquery'; arr['css']='oocss'; var obj={}; for(var i in arr) { obj[i]=arr[i]; } ...
- 区分jquery中的offset和position
一次又一次地碰到需要获取元素位置的问题, 然后一次又一次地查offset和position的区别. 忍不了了, 这次一定得想办法记下来. position是元素相对于父元素的位置. 这个好记, par ...
- JVM内存管理(一)
方法区: 方法区存放了要加载的类的信息(名称.修饰符等).类的静态变量.类中定义为final类型的常量.类中的field信息.类中的方法信息.当开发人员在程序中通过Class对象的getName.is ...