UVALive 4119 Always an integer (差分数列,模拟)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
Always an integer
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Combinatorics is a branch of mathematics chiefly concerned with counting discrete objects. For instance, how many ways can you pick two people out of a crowd of n people? Into how many regions can you divide a circular disk by connecting n points on its boundary with one another? How many cubes are in a pyramid with square layers ranging from 1×1 to n×n cubes?

Many questions like these have answers that can be reduced to simple polynomials in n . The answer to the first question above is n(n - 1)/2 , or (n
2 - n)/2 . The answer to the second is (n
4 - 6n
3 + 23n
2 - 18n + 24)/24 . The answer to the third is n(n + 1)(2n + 1)/6 , or (2n
3 + 3n
2 + n)/6 . We write these polynomials in a standard form, as a polynomial with integer coefficients divided by a positive integer denominator.
These polynomials are answers to questions that can have integer answers only. But since they have fractional coefficients, they look as if they could produce non-integer results! Of course, evaluating these particular polynomials on a positive integer always results in an integer. For other polynomials of similar form, this is not necessarily true. It can be hard to tell the two cases apart. So that, naturally, is your task.
Input
The input consists of multiple test cases, each on a separate line. Each test case is an expression in the form (P)/D , where P is a polynomial with integer coefficients and D is a positive integer denominator. P is a sum of terms of the form Cn
E , where the coefficient C and the exponent E satisfy the following conditions:
- E is an integer satisfying 0
E
100 . If E is 0, then Cn
E is expressed as C . If E is 1, then Cn
E is expressed as Cn , unless C is 1 or -1. In those instances, Cn
E is expressed as n or - n . - C is an integer. If C is 1 or -1 and E is not 0 or 1, then the Cn
E will appear as n
E or - n
E . - Only non-negative C values that are not part of the first term in the polynomial are preceded by +.
- Exponents in consecutive terms are strictly decreasing.
- C and D fit in a 32-bit signed integer.
See the sample input for details.
Input is terminated by a line containing a single period.
Output
For each test case, print the case number (starting with 1). Then print `Always an integer' if the test case polynomial evaluates to an integer for every positive integer n . Print ` Not always an integer' otherwise. Print the output for separate test cases on separate lines. Your output should follow the same format as the sample output.
Sample Input
(n^2-n)/2
(2n^3+3n^2+n)/6
(-n^14-11n+1)/3
.
Sample Output
Case 1: Always an integer
Case 2: Always an integer
Case 3: Not always an integer
题目的意思是让你判断一个整系数多项式的值是否一直都能被一个所给的正整数所整除。
通过对差分数列的不断求导,我们可以发现,对于任意多项式P,我们只需要判断从1到k+1是否满足就行了,其中,k为多项式P中的最高次数。
接下来就是纯模拟了。
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;
string s;
vector<pair<long long,long long> >vec;
long long fast_mod(long long m,long long n,long long fenm)
{
long long ret=;
long long temp=m;
while(n)
{
if(n&)
{
ret=ret*temp;
ret%=fenm;
}
temp=temp*temp;
temp%=fenm;
n>>=;
}
return ret;
}
int main()
{
ios::sync_with_stdio(false);
int cas=;
//freopen("in.in","r",stdin);
while(cin>>s)
{
if(s==".")break;
int len=s.length();
vec.clear();
int pos=;
while(pos<len&&s[pos]!='/')pos++;
long long fenm=;
int index=pos+;
while(index<len) fenm=s[index++]-''+fenm*;
if(fenm==)fenm=;
long long a,b;
long long maxx=;
bool chac=;
for(int i=;i<pos;)
{
chac=;
a=,b=;
if(s[i]=='(')i++;
if(s[i]==')'||s[i]=='/')break;
if(s[i]=='+'||s[i]=='-')
{
if(s[i]=='-')chac=;
i++;
}
while(i<pos&&s[i]!='/'&&s[i]!='n'&&s[i]!=')')
{
a*=;
a+=s[i++]-'';
}
if(a==)a=;
if(chac)a*=-;
if(s[i]=='/'||s[i]==')')
{
vec.push_back(make_pair(a,));
break;
}
i++;
if(s[i]=='^')i++;
while(i<pos&&s[i]>=''&&s[i]<='')
{
b*=;
b+=s[i++]-'';
}
if(b==)b=;
vec.push_back(make_pair(a,b));
maxx=max(b,maxx);
}
bool flag=;
long long temp;
for(int i=;i<=maxx+;i++)
{
temp=;
for(int j=;j<vec.size();j++)
{
temp+=(vec[j].first*fast_mod(i,vec[j].second,fenm))%fenm;
temp%=fenm;
}
if(temp){flag=;break;}
}
cout<<"Case "<<cas++<<": ";
if(flag)cout<<"Not always an integer"<<endl;
else cout<<"Always an integer"<<endl;
s.clear();
}
return ;
}
UVALive 4119 Always an integer (差分数列,模拟)的更多相关文章
- LA 4119 Always an integer (数论+模拟)
ACM-ICPC Live Archive 一道模拟题,题意是问一个给出的多项式代入正整数得到的值是否总是整数. 这题是一道数论题,其实对于这个式子,我们只要计算1~最高次项是否都满足即可. 做的时候 ...
- CF460C Present (二分 + 差分数列)
Codeforces Round #262 (Div. 2) C C - Present C. Present time limit per test 2 seconds memory limit p ...
- hdu4970 Killing Monsters (差分数列)
2014多校9 1011 http://acm.hdu.edu.cn/showproblem.php?pid=4970 Killing Monsters Time Limit: 2000/1000 M ...
- uvalive 4119 Always an Interger
差分数列+字符串处理 题意:是让你判断一个整系数多项式的值是否一直都能被一个所给的正整数所整除. 通过对差分数列的不断求导,我们可以发现,对于任意多项式P,我们只需要判断n从1到k+1是否满足就行了, ...
- [CF 295A]Grag and Array[差分数列]
题意: 有数列a[ ]; 操作op[ ] = { l, r, d }; 询问q[ ] = { x, y }; 操作表示对a的[ l, r ] 区间上每个数增加d; 询问表示执行[ x, y ]之间的o ...
- [CF 276C]Little Girl and Maximum Sum[差分数列]
题意: 给出n项的数列A[ ], q个询问, 询问 [ l, r ] 之间项的和. 求A的全排列中该和的最大值. 思路: 记录所有询问, 利用差分数列qd[ ], 标记第 i 项被询问的次数( 每次区 ...
- LA 4119 (差分数列 多项式) Always an integer
题意: 给出一个形如(P)/D的多项式,其中P是n的整系数多项式,D为整数. 问是否对于所有的正整数n,该多项式的值都是整数. 分析: 可以用数学归纳法证明,若P(n)是k次多项式,则P(n+1) - ...
- Always an integer UVALive - 4119
题目很简单,就是求表达式(P/D)的结果是不是整数.其中P是一个整系数的多项式,D是一个正整数. 把1-k(最高次)+1都试一次就好了.结论可以总结归纳得到.(k取 0, 1, 2 .... 的情况推 ...
- UVALive 5888 Stack Machine Executor (栈+模拟)
Stack Machine Executor 题目链接: http://acm.hust.edu.cn/vjudge/problem/26636 Description http://7xjob4.c ...
随机推荐
- php中对象的串行化
我们大家有知道PHP串行化可以把变量包括对象,转化成连续bytes数据,你可以将串行化后的变量存在一个文件里或在网络上传输,然后再反串行化还原为原来的数据.文章这里就PHP串行化为大家详细的介绍.你在 ...
- 使用pip install 或者easy_install安装Python的各种包出现cc failed with exit status 1
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- java中连接postgresql基本代码
try { Class.forName( "org.postgresql.Driver" ).newInstance(); String url = "jdbc:post ...
- XML Schema <第三篇>
验证XML文档是否符合议定的XML结构有两种方法,分别是DTD模式与XML Schema.本文主要介绍XML Schema. 一.XML Schema的优点 XML Schema基于XML,没有专门的 ...
- AOP技术基础
1.引言 2.AOP技术基础 3.Java平台AOP技术研究 4..Net平台AOP技术研究 2.1 AOP技术起源 AOP技术的诞生并不算晚,早在1990年开始,来自Xerox Palo Alto ...
- curl 网页抓取
如果要把这个网页保存下来,可以使用-o参数,这就相当于使用wget命令了. curl -o [文件名] www.tvbs.cc 二.自动跳转 有的网址是自动跳转的.使用-L参数,curl就会跳转到新的 ...
- c# 编程添加控件
Button b = new Button();//创建一个新的按钮 b.Text = "test"; //添加到panel1中 panel1.Controls.Add(b);
- 实用的VIM配置文件
VIM配置文件名为.vimrc,默认在用户根目录下,或者在命令模式下输入:version可以获取配置文件路径. 在VIM命令行下输入options,然后回车,可以查看VIM所有的参数选项. 双引号&q ...
- mongodb----修改器
$inc:增加或者减少指定键值,如果键不存在,就创建一个键. $set:指定一个健的值,如果键不存在,就创建一个键. $unset:删除指定的键. $push:向指定的数组末尾加添加一个元素,如果数组 ...
- seajs教程之seajs学习笔记 seajs.use用法
seajs.use 用来在页面中加载模块.通过 use 方法,可以在页面中加载任意模块. 实例地址:http://www.android100.org/html/201405/23/12807.htm ...