uva1069 - Always an integer
来源:互联网 发布:沈阳智能云科 知乎 编辑:程序博客网 时间:2024/05/05 10:38
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 ofn people? Into how many regions can you divide a circular disk by connectingn points on its boundary with one another? How many cubes are in a pyramid with square layers ranging from1×1 to n×n cubes?
Many questions like these have answers that can be reduced to simple polynomials inn. The answer to the first question above is n(n - 1)/2, or (n2 -n)/2. The answer to the second is (n4 - 6n3 + 23n2 - 18n + 24)/24. The answer to the third isn(n + 1)(2n + 1)/6, or (2n3 + 3n2 + 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 andD is a positive integer denominator. P is a sum of terms of the form CnE, where the coefficient C and the exponent E satisfy the following conditions:
- E is an integer satisfying 0E100. If E is 0, then CnE is expressed as C. If E is 1, thenCnE is expressed asCn, unless C is 1 or -1. In those instances,CnE is expressed asn or - n.
- C is an integer. If C is 1 or -1 andE is not 0 or 1, then the CnE will appear as nE or- nE.
- 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 integern. 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
给你一个多项式,问n取任何值的时候这个多项式的值是否都是整数。
设最高项数为k。当k=0时,不存在n,只要计算P(1)是否为整数。当k=1时,设P(n)=a*n+b,P(n+1)-P(n)=a,是等差数列,只要首项和公差都是D的倍数,n取任何值P都是d的倍数,也就是只要P(1)和P(2)是D的倍数就行。当k=3时,设P(n)=a*n^2+b*n+c,P(n+1)-P(n)=2an+a+b,因为2an+a+b是n的一次多项式,只有当n=1和n=2时都是D的倍数的时候2an+a+b才能被D整除,也就是P(1),P(2)-P(1),P(3)-P(2)都要是D的倍数,因此只要满足P(1),P(2),P(3)都是D的倍数就行。
根据数学归纳法,只要n的取值从1...k+1的P(n)都是D的倍数,n取任何值都会是D的倍数。
训练指南上还有个差分数列的图,写的非常清楚。。
我用数据结构里面那个表达式求值的方法写的,代码略坑,还有就是昨天数据结构考的跟翔一样
#include<iostream>#include<queue>#include<cstring>#include<cstdio>#include<cmath>#include<set>#include<map>#include<vector>#include<stack>#include<algorithm>#define eps 1e-9#define MAXN 2010#define MOD 1000000007using namespace std;int isp[100],icp[100],L,D; //isp栈内优先级 icp栈外优先级char str[MAXN],s[MAXN];void init(){ isp['#']=0; isp['(']=1; isp['*']=isp['/']=5; isp['+']=isp['-']=3; isp['^']=7; isp[')']=8; icp['#']=0; icp['(']=8; icp['*']=icp['/']=4; icp['+']=icp['-']=2; icp['^']=6; icp[')']=1;}long long cal(long long x1,long long x2,char op){ switch(op){ case('+'):return (x1+x2)%D; case('-'):return (x1-x2)%D; case('*'):return (x1*x2)%D; case('^'): long long ret=1; while(x2--) ret=(ret*x1)%D; return ret; }}int check(int x){ stack<char> st; stack<long long> st2; st.push('#'); st2.push(0); int i=0; while(str[i]!='/'){ if(str[i]=='n'){ st2.push(x); i++; } if(isdigit(str[i])){ long long m=0; while(isdigit(str[i])){ m=m*10+str[i]-'0'; i++; } st2.push(m); } char op=st.top(); if(icp[str[i]]>isp[op]){ st.push(str[i]); i++; } else if(icp[str[i]]<isp[op]){ long long num1=st2.top(); st2.pop(); long long num2=st2.top(); st2.pop(); long long t=cal(num2,num1,op); st.pop(); st2.push(t); } else{ st.pop(); i++; } } if(st2.top()%D==0) return 1; return 0;}int main(){ freopen("in.txt","r",stdin); init(); int cas=0; while(scanf("%s",s),s[0]!='.'){ //加上最开始的+(如果有)和* int k=0; for(int i=0;s[i];i++){ str[k++]=s[i]; if(isdigit(s[i])&&s[i+1]=='n') str[k++]='*'; if(s[i]=='('&&isdigit(s[i+1])) str[k++]='+'; } str[k]=0; L=strlen(str); //最高项 int K=1; for(int i=0;i<L;i++) if(str[i]=='^'){ int j=i+1,m=0; while(isdigit(str[j])){ m=m*10+str[j]-'0'; j++; } K=m+1; break; } //算出除数 for(int i=0;i<L;i++) if(str[i]=='/'){ int j=i+1,m=0; while(isdigit(str[j])){ m=m*10+str[j]-'0'; j++; } D=m; break; } int flag=1; for(int i=1;i<=K;i++) if(!check(i)){ flag=0; break; } printf("Case %d: ",++cas); if(flag) printf("Always an integer\n"); else printf("Not always an integer\n"); } return 0;}
- uva1069 - Always an integer
- uva1069 Always an integer【解法一】
- uva1069 Always an integer【解法二】
- Always an Integer LA4119
- worldfinals 2008 Always an integer
- uvaoj 1069 Always an integer
- uva 1069 - Always an integer(数学+暴力)
- UVA 1069 - Always an integer(数论)
- UVA - 1069 Always an integer (模拟)
- LA 4119 Always an integer (数学)
- UVALive 4119 Always an integer(差分数列+模拟)
- always @ (*)
- uva1069-数学归纳法
- switch quantity not an integer
- count_inversions in an integer list
- Reverse digits of an integer.
- reverse digits of an integer.
- Reverse digits of an integer.
- mysql日期加减
- java相关网站
- windows压缩zip,linux解压乱码
- lucene检索简单例子
- Jquery获取浏览器宽高的代码
- uva1069 - Always an integer
- DataPump关于跨数据库版本remove数据
- linux 系统调用
- 自己的学习日记 流程控制语句1
- MongoDB学习之旅二:MongoDB安装及配置
- 完整版 VHDL设计数字电子时钟
- 静态服务器分担带宽的理解
- nodejs 安装步骤和打包命令
- Java反射机制