uva1069 Always an integer【解法二】
2016-12-17 17:28
411 查看
Combinatorics is a branch of mathematics chie y 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
an- other? 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 rst
question above is n ( n
解法一【随机化】见【这里】。
设最高次数为k,只需要代入n=1,2,…,k+1验证。
证明如下:
k=0,显然只需要带入p(1)。
k>=1,可以发现首项和相邻两项的差都能整除d就是整个数列能整除d的充要条件。差分数列f(n)=p(n+1)-p(n)是一个k-1次项式,根据归纳法可以知道需要需要保证f(1),f(2),..,f(k)全能整除d,再加上p(1)能整除d,也就是p(1),p(2),…,p(k+1)能整除d。
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
an- other? 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 rst
question above is n ( n
解法一【随机化】见【这里】。
设最高次数为k,只需要代入n=1,2,…,k+1验证。
证明如下:
k=0,显然只需要带入p(1)。
k>=1,可以发现首项和相邻两项的差都能整除d就是整个数列能整除d的充要条件。差分数列f(n)=p(n+1)-p(n)是一个k-1次项式,根据归纳法可以知道需要需要保证f(1),f(2),..,f(k)全能整除d,再加上p(1)能整除d,也就是p(1),p(2),…,p(k+1)能整除d。
#include<cstdio> #include<cstring> #include<cstdlib> #define LL long long const int T=10000; LL a[110],t[110],d,r[10010]; char s[100010]; int n; bool init() { int x,f,p=0; char c; scanf("%s",s); c=s[p++]; if (c=='.') return 0; n=0; c=s[p++]; while (c!=')') { n++; if (c=='-') { f=-1; c=s[p++]; } else f=1; if (c=='+') c=s[p++]; x=0; while (c>='0'&&c<='9') { x=x*10+c-'0'; c=s[p++]; } if (!x) x=1; x*=f; a =x; if (c!='n') { t =0; continue; } c=s[p++]; x=0; if (c=='^') { c=s[p++]; while (c>='0'&&c<='9') { x=x*10+c-'0'; c=s[p++]; } } else x=1; t =x; } c=s[p++]; c=s[p++]; x=0; while (c>='0'&&c<='9') { x=x*10+c-'0'; c=s[p++]; } d=x; return 1; } LL pow(LL base,LL p) { LL ret=1; while (p) { if (p&1) ret=ret*base%d; base=base*base%d; p>>=1; } return ret; } bool solve() { int i,j; LL x; for (i=1;i<=T;i++) { x=0; for (j=1;j<=n;j++) x=((x+pow(r[i],t[j])*a[j]%d)%d+d)%d; if (x) return 0; } return 1; } void make() { int i; srand(123); for (i=1;i<=T;i++) r[i]=rand(); } int main() { int K=0; make(); while (init()) { printf("Case %d: ",++K); if (solve()) printf("Always an integer\n"); else printf("Not always an integer\n"); } }
相关文章推荐
- uva1069 Always an integer【解法一】
- uva1069 - Always an integer
- UVA - 1069 Always an integer (模拟)
- UVA 1069 - Always an integer(数论)
- uva 1069 - Always an integer(数学+暴力)
- UVA 1069 - Always an integer(数论)
- UVALive 4119 Always an integer (差分数列,模拟)
- UVALive 4119 Always an integer(差分数列+模拟)
- uvaoj 1069 Always an integer
- LA 4119 Always an integer (数学)
- 2008 world final B (Always an Integer)
- LA 4119 Always an integer (数论+模拟)
- worldfinals 2008 Always an integer
- LA 4119 (差分数列 多项式) Always an integer
- LA 4119 - Always an integer
- LA4119 -- Always an integer
- 【降维解法:最大字段和->最大子矩阵和->最终版最大子长方体和】【UVA10755】Garbage Heap
- Uva-1228 Integer Transmission(贪心+DP)
- UVA - 11624 Fire!(两次BFS 两种解法)
- Integer Game(UVA11489)3的倍数