HDU 5847 Different Sums (构造+数学+打表)
2016-08-18 02:12
495 查看
题意:构造长度为n的序列;满足两个条件:
1、没有连续子序列和相等
2、每个元素都要小于3n+18
多校九这场最后700+提交,但0人通过。
大家都想茬了……或者把题目想简单了,确实很难构造!
n最大只有2000;肯定想到三重循环暴力打表,但是如果升序从1开始,发现2000的时候最后一个是6853(还是多少)超过了3n+18,实际上300多就不行了。升序会超过那我降序打表啊,从3n+17开始,但是发现从20几(好像是)开始就凑不齐n个数了。所以最后的序列不是有序的,这不看题解鬼想的到怎么构造
DifferentSums
Tag: Construction, Math
For small n, it’s easy problem.
Let S[i] be the sum of first i numbers.
We take a prime number p larger than n, andan integer x (0 <= x < p), make S[i] = 2 * i * p + (i * (i+1) / 2 * x) %p. Let’s define r[i] = (i * (i+1) / 2 * x) % p.
If S[i] – S[j] = S[k] – S[l], then i – j =k – l because |r[i] -r[j]| < p and |r[k] – r[l]| < p. Also (r[i] – r[j] –r[k] + r[l]) must be divisible by p, this leads to i + j = k + l. It means thati = k and j = l.
Thus, the array that has these subsumssatisfies the problem statement.
Now we must make all integers in the array lessthan 3*n+18.
We can choose p as the smallest prime largerthan n, and select x that minimizes max{S[i]-S[i-1]}.
For all 6 <= n <= 2000, if we choose xoptimally, the maximum value of S[i]-S[i-1] is less than 3*n+18.
即最后的ans[i] = 2*p +( r[i] * x ) % p -
( r[i-1] * x ) % p;p是一个大于n的素数,x是一个系数,r[i]=i(i+1)/2。
证明见上
为了试ans[i]尽可能小,让p是第一个大于n的素数,而x使得( r[i] * x ) % p - ( r[i-1] * x ) % p的最大值最小。
在求x上我花了很大的功夫,一开始想推公式,没什么进展,最后暴力把[1,2000]对应的x打表
【代码】
1、没有连续子序列和相等
2、每个元素都要小于3n+18
多校九这场最后700+提交,但0人通过。
大家都想茬了……或者把题目想简单了,确实很难构造!
n最大只有2000;肯定想到三重循环暴力打表,但是如果升序从1开始,发现2000的时候最后一个是6853(还是多少)超过了3n+18,实际上300多就不行了。升序会超过那我降序打表啊,从3n+17开始,但是发现从20几(好像是)开始就凑不齐n个数了。所以最后的序列不是有序的,这不看题解鬼想的到怎么构造
DifferentSums
Tag: Construction, Math
For small n, it’s easy problem.
Let S[i] be the sum of first i numbers.
We take a prime number p larger than n, andan integer x (0 <= x < p), make S[i] = 2 * i * p + (i * (i+1) / 2 * x) %p. Let’s define r[i] = (i * (i+1) / 2 * x) % p.
If S[i] – S[j] = S[k] – S[l], then i – j =k – l because |r[i] -r[j]| < p and |r[k] – r[l]| < p. Also (r[i] – r[j] –r[k] + r[l]) must be divisible by p, this leads to i + j = k + l. It means thati = k and j = l.
Thus, the array that has these subsumssatisfies the problem statement.
Now we must make all integers in the array lessthan 3*n+18.
We can choose p as the smallest prime largerthan n, and select x that minimizes max{S[i]-S[i-1]}.
For all 6 <= n <= 2000, if we choose xoptimally, the maximum value of S[i]-S[i-1] is less than 3*n+18.
即最后的ans[i] = 2*p +( r[i] * x ) % p -
( r[i-1] * x ) % p;p是一个大于n的素数,x是一个系数,r[i]=i(i+1)/2。
证明见上
为了试ans[i]尽可能小,让p是第一个大于n的素数,而x使得( r[i] * x ) % p - ( r[i-1] * x ) % p的最大值最小。
在求x上我花了很大的功夫,一开始想推公式,没什么进展,最后暴力把[1,2000]对应的x打表
【代码】
/* *********************************************** Author :angon ************************************************ */ #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <stack> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; #define showtime fprintf(stderr,"time = %.15f\n",clock() / (double)CLOCKS_PER_SEC) #define lld %I64d #define REP(i,k,n) for(int i=k;i<n;i++) #define REPP(i,k,n) for(int i=k;i<=n;i++) #define scan(d) scanf("%d",&d) #define scanl(d) scanf("%I64d",&d) #define scann(n,m) scanf("%d%d",&n,&m) #define scannl(n,m) scanf("%I64d%I64d",&n,&m) #define mst(a,k) memset(a,k,sizeof(a)) #define LL long long #define mod 1000000007 inline int read(){int s=0;char ch=getchar();for(; ch<'0'||ch>'9'; ch=getchar());for(; ch>='0'&&ch<='9'; ch=getchar())s=s*10+ch-'0';return s;} int r[2005]; const int N = 100000; int prime = {0},num_prime = 0; int isNotPrime = {1, 1}; void get_prime() { for(long i = 2 ; i < N ; i ++) { if(! isNotPrime[i]) prime[num_prime ++]=i; for(long j = 0 ; j < num_prime && i * prime[j] < N ; j ++) { isNotPrime[i * prime[j]] = 1; if( !(i % prime[j] ) ) break; } } } /* 打表 int vis[6018*2001],ans[2005]; int ck(int n) { for(int i=1;i<=n;i++) { if(ans[i]>=3*(n+6)) { printf("有大于3*(n+6)的数\n:n=%d %d\n",n,ans[i]); return 0; } } //vis.clear(); mst(vis,0); for(int i=n;i>=1;i--) { LL sum=0; for(int k=i;k>=1;k--) { sum+=ans[k]; if(vis[sum]>0) { printf("有重复和:%I64d n=%d\n",sum,n); return 0; } vis[sum]=1; } } printf("yes\n"); return 1; } */ int x[2005]={0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,10,3,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,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1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); r[0]=0; get_prime(); for(int i=1;i<=2000;i++) r[i]=r[i-1]+i; int t,n; scan(t); while(t--) { scan(n); //for(n=1;n<=2000;n++){ int p = upper_bound(prime,prime+num_prime,n) - prime; p=prime[p]; /* 打表 for(int j=1;j<p;j++) { for(int i=1;i<=n;i++) { ans[i] = 2*p + r[i]*j%p - r[i-1]*j%p; //printf("%d%c",ans[i],i==n?'\n':' '); } if(ck(n)==0) continue; printf("%d,",j); break; } } */ for(int i=1;i<=n;i++) { int T = 2*p + r[i]*x %p - r[i-1]*x %p; printf("%d%c",T,i==n?'\n':' '); //ans[i]=T; } //if(!ck(n)) printf("%d\n",n);} } return 0; }
相关文章推荐
- seajs 2.x _ 定义基本模块
- [Web前端工具篇]Sublime 3安装Markdown插件
- 在html中使用javascript
- [React Fundamentals] Composable Components
- json字符串,javaScript,java之间的转换
- 入门 Webpack
- [leetcode]-Linked List Random Node
- [leetcode]-Linked List Random Node
- [leetcode]-Linked List Random Node
- [leetcode]-Linked List Random Node
- jsp有哪些内置对象?作用分别是什么?分别有什么方法?
- Gson 框架的使用
- 数学推导______Different Sums( hdu 5847 2016多校第九场)
- JQ验证表单
- node之hello world
- 剑指offer_04 字符串替换
- Rapidjson的简单使用(拼接json串,存取文件中的json,解析json串)
- 一次五分钟 angularJS (1)—— Binding
- 基于html基础上的html5新特性
- JQuery强化教程 —— jQuery Easing