Poj 3370
题目传送门:https://vjudge.net/problem/POJ-3370
题意:在n个数中找K个数使得他们的和为c的倍数。
题解:抽屉原理,同poj 2356 只不过写法上有所简化。
简化版:

1 //#include<bits/stdc++.h>
2 #include<time.h>
3 #include <set>
4 #include <map>
5 #include <stack>
6 #include <cmath>
7 #include <queue>
8 #include <cstdio>
9 #include <string>
10 #include <vector>
11 #include <cstring>
12 #include <utility>
13 #include <cstring>
14 #include <iostream>
15 #include <algorithm>
16 #include <list>
17 using namespace std;
18 #define eps 1e-10
19 #define PI acos(-1.0)
20 #define lowbit(x) ((x)&(-x))
21 #define zero(x) (((x)>0?(x):-(x))<eps)
22 #define mem(s,n) memset(s,n,sizeof s);
23 #define ios {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
24 typedef long long ll;
25 typedef unsigned long long ull;
26 const int maxn=1e5+5;
27 const int Inf=0x7f7f7f7f;
28 const ll Mod=999911659;
29 //const int N=3e3+5;
30 bool isPowerOfTwo(int n) { return n > 0 && (n & (n - 1)) == 0; }//判断一个数是不是 2 的正整数次幂
31 int modPowerOfTwo(int x, int mod) { return x & (mod - 1); }//对 2 的非负整数次幂取模
32 int getBit(int a, int b) { return (a >> b) & 1; }// 获取 a 的第 b 位,最低位编号为 0
33 int Max(int a, int b) { return b & ((a - b) >> 31) | a & (~(a - b) >> 31); }// 如果 a>=b,(a-b)>>31 为 0,否则为 -1
34 int Min(int a, int b) { return a & ((a - b) >> 31) | b & (~(a - b) >> 31); }
35 ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
36 ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
37 int Abs(int n) {
38 return (n ^ (n >> 31)) - (n >> 31);
39 /* n>>31 取得 n 的符号,若 n 为正数,n>>31 等于 0,若 n 为负数,n>>31 等于 -1
40 若 n 为正数 n^0=n, 数不变,若 n 为负数有 n^(-1)
41 需要计算 n 和 -1 的补码,然后进行异或运算,
42 结果 n 变号并且为 n 的绝对值减 1,再减去 -1 就是绝对值 */
43 }
44 ll binpow(ll a, ll b,ll c) {
45 ll res = 1;
46 while (b > 0) {
47 if (b & 1) res = res * a%c;
48 a = a * a%c;
49 b >>= 1;
50 }
51 return res%c;
52 }
53 void extend_gcd(ll a,ll b,ll &x,ll &y)
54 {
55 if(b==0) {
56 x=1,y=0;
57 return;
58 }
59 extend_gcd(b,a%b,x,y);
60 ll tmp=x;
61 x=y;
62 y=tmp-(a/b)*y;
63 }
64 ll mod_inverse(ll a,ll m)
65 {
66 ll x,y;
67 extend_gcd(a,m,x,y);
68 return (m+x%m)%m;
69 }
70 ll eulor(ll x)
71 {
72 ll cnt=x;
73 ll ma=sqrt(x);
74 for(int i=2;i<=ma;i++)
75 {
76 if(x%i==0) cnt=cnt/i*(i-1);
77 while(x%i==0) x/=i;
78 }
79 if(x>1) cnt=cnt/x*(x-1);
80 return cnt;
81 }
82 ll c,n,a[maxn],pos[maxn],sum;
83 int main()
84 {
85 while(scanf("%lld%lld",&c,&n),c||n){
86 sum=0;
87 for(int i=0;i<n;i++)
88 {
89 scanf("%lld",&a[i]);
90 pos[i]=-2;
91 }
92 pos[0]=-1;//整除情况也包括了.
93 for(int i=0;i<n;i++)
94 {
95 sum+=a[i];
96 if(pos[sum%c]!=-2)
97 {
98 //cout<<i-pos[sum[i]%c]<<endl;
99 for(int j=pos[sum%c]+1;j<=i;j++)
100 {
101 cout<<j+1;
102 if(i!=j) cout<<" ";
103 }
104 printf("\n");
105 break;
106 }
107 pos[sum%c]=i;
108 }
109 }
110 return 0;
111 }
基础版:

1 #include<time.h>
2 #include <set>
3 #include <map>
4 #include <stack>
5 #include <cmath>
6 #include <queue>
7 #include <cstdio>
8 #include <string>
9 #include <vector>
10 #include <cstring>
11 #include <utility>
12 #include <cstring>
13 #include <iostream>
14 #include <algorithm>
15 #include <list>
16 using namespace std;
17 #define eps 1e-10
18 #define PI acos(-1.0)
19 #define lowbit(x) ((x)&(-x))
20 #define zero(x) (((x)>0?(x):-(x))<eps)
21 #define mem(s,n) memset(s,n,sizeof s);
22 #define ios {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
23 typedef long long ll;
24 typedef unsigned long long ull;
25 const int maxn=1e6+5;
26 const int Inf=0x7f7f7f7f;
27 const ll Mod=999911659;
28 //const int N=3e3+5;
29 bool isPowerOfTwo(int n) { return n > 0 && (n & (n - 1)) == 0; }//判断一个数是不是 2 的正整数次幂
30 int modPowerOfTwo(int x, int mod) { return x & (mod - 1); }//对 2 的非负整数次幂取模
31 int getBit(int a, int b) { return (a >> b) & 1; }// 获取 a 的第 b 位,最低位编号为 0
32 int Max(int a, int b) { return b & ((a - b) >> 31) | a & (~(a - b) >> 31); }// 如果 a>=b,(a-b)>>31 为 0,否则为 -1
33 int Min(int a, int b) { return a & ((a - b) >> 31) | b & (~(a - b) >> 31); }
34 ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
35 ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
36 int Abs(int n) {
37 return (n ^ (n >> 31)) - (n >> 31);
38 /* n>>31 取得 n 的符号,若 n 为正数,n>>31 等于 0,若 n 为负数,n>>31 等于 -1
39 若 n 为正数 n^0=n, 数不变,若 n 为负数有 n^(-1)
40 需要计算 n 和 -1 的补码,然后进行异或运算,
41 结果 n 变号并且为 n 的绝对值减 1,再减去 -1 就是绝对值 */
42 }
43 ll binpow(ll a, ll b,ll c) {
44 ll res = 1;
45 while (b > 0) {
46 if (b & 1) res = res * a%c;
47 a = a * a%c;
48 b >>= 1;
49 }
50 return res%c;
51 }
52 void extend_gcd(ll a,ll b,ll &x,ll &y)
53 {
54 if(b==0) {
55 x=1,y=0;
56 return;
57 }
58 extend_gcd(b,a%b,x,y);
59 ll tmp=x;
60 x=y;
61 y=tmp-(a/b)*y;
62 }
63 ll mod_inverse(ll a,ll m)
64 {
65 ll x,y;
66 extend_gcd(a,m,x,y);
67 return (m+x%m)%m;
68 }
69 ll eulor(ll x)
70 {
71 ll cnt=x;
72 ll ma=sqrt(x);
73 for(int i=2;i<=ma;i++)
74 {
75 if(x%i==0) cnt=cnt/i*(i-1);
76 while(x%i==0) x/=i;
77 }
78 if(x>1) cnt=cnt/x*(x-1);
79 return cnt;
80 }
81 ll c,n,a[maxn],sum[maxn],pos[maxn];
82 int main()
83 {
84 while(scanf("%lld%lld",&c,&n),n||c){
85 mem(pos,0);
86 mem(sum,0);
87 for(int i=1;i<=n;i++)
88 {
89 scanf("%lld",&a[i]);
90 sum[i]=sum[i-1]+a[i];
91 }
92 for(int i=1;i<=n;i++)
93 {
94 if(sum[i]%c==0)
95 {
96 //cout<<i<<endl;
97 for(int j=1;j<=i;j++) cout<<j<<" ";
98 cout<<endl;
99 break;
100 }
101 if(pos[sum[i]%c])
102 {
103 //cout<<i-pos[sum[i]%c]<<endl;
104 for(int j=pos[sum[i]%c]+1;j<=i;j++) cout<<j<<" ";
105 cout<<endl;
106 break;
107 }
108 pos[sum[i]%c]=i;
109 }
110 }
111 return 0;
112 }
Poj 3370的更多相关文章
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- [POJ 3370] Halloween treats
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7143 Accepted: 2641 ...
- POJ 2356 && POJ 3370 鸽巢原理
POJ 2356: 题目大意: 给定n个数,希望在这n个数中找到一些数的和是n的倍数,输出任意一种数的序列,找不到则输出0 这里首先要确定这道题的解是必然存在的 利用一个 sum[i]保存前 i 个数 ...
- POJ 3370 Halloween treats( 鸽巢原理简单题 )
链接:传送门 题意:万圣节到了,有 c 个小朋友向 n 个住户要糖果,根据以往的经验,第i个住户会给他们a[ i ]颗糖果,但是为了和谐起见,小朋友们决定要来的糖果要能平分,所以他们只会选择一部分住户 ...
- poj 3370 Halloween treats(鸽巢原理)
Description Every year there is the same problem at Halloween: Each neighbour is only willing to giv ...
- poj 3370 鸽笼原理知识小结
中学就听说过抽屉原理,可惜一直没机会见识,现在这题有鸽笼原理的结论,但其实知不知道鸽笼原理都可以做 先总结一下鸽笼原理: 有n+1件或n+1件以上的物品要放到n个抽屉中,那么至少有一个抽屉里有两个或两 ...
- 鸽巢原理应用-分糖果 POJ 3370 Halloween treats
基本原理:n+1只鸽子飞回n个鸽笼至少有一个鸽笼含有不少于2只的鸽子. 很简单,应用却也很多,很巧妙,看例题: Description Every year there is the same pro ...
- POJ 3370 Halloween treats(抽屉原理)
Halloween treats Every year there is the same problem at Halloween: Each neighbour is only willing t ...
- POJ 3370 Halloween treats(抽屉原理)
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6631 Accepted: 2448 ...
随机推荐
- Spring应用上下文生命周期
Spring应用上下文生命周期整体分成四个阶段 ConfigurableApplicationContext#refresh,加载或者刷新持久化配置 ConfigurableApplicationCo ...
- 016.NET5_MVC_视图组件扩展定制
视图组件 1. 呈现页面响应的某一部分而不是整个响应 2. 包括在控制器和视图之间发生的关注分类和可测试优势 3.可以具有参数和业务逻辑 4. 通常在页面局部调用 如何自定义视图组件? 1.Razor ...
- Linux bash script regex auto replace
Linux bash script regex auto replace 自动替换 /assets/css/0.styles.96df394b.css => ./assets/css/0.sty ...
- Taro 版本
Taro 版本 https://taro-docs.jd.com/taro/versions.html 1.x 1.3.34 https://taro-docs.jd.com/taro/docs/1. ...
- Techme Inc热心公益事业 积极开展公益活动
从2015年起,Techme inc(公司编号:20151524696)便通过优质的产品和服务,帮助顾客实现营养与健康的目标.与此同时,Techme inc(公司编号:20151524696)多年来始 ...
- SSL/TLS协议详解(上):密码套件,哈希,加密,密钥交换算法
本文转载自SSL/TLS协议详解(上):密码套件,哈希,加密,密钥交换算法 导语 作为一名安全爱好者,我一向很喜欢SSL(目前是TLS)的运作原理.理解这个复杂协议的基本原理花了我好几天的时间,但只要 ...
- ipv4ipv6 地址字符串表示最大长度
1 for IPV4 #define INET_ADDRSTRLEN 16 111.112.113.114 32位IPV4地址,使用10进制+句点表示时,所占用的char数组的长度为16,其中包括最后 ...
- Java ThreadPoolExecutor详解
ThreadPoolExecutor是Java语言对于线程池的实现.池化技术是一种复用资源,减少开销的技术.线程是操作系统的资源,线程的创建与调度由操作系统负责,线程的创建与调度都要耗费大量的资源,其 ...
- 推荐一款好用的免费远程控制软件——ToDesk
创作立场声明:我在本文中评测的软件为自用,感觉不错并且全免费,第一时间发出来和大家分享,欢迎理性观点交流碰撞. 疫情刚开始的时候,待在家里不能上班,但是还是有很多工作需要在线完成,常常需要跑回办公室拿 ...
- CF1491C Pekora and Trampoline 题解
题目链接 比赛时只想到了 \(\mathcal O(n^3)\) 的暴力做法,官方题解是 \(\mathcal O(n^2)\) ,并且是可以优化为 \(\mathcal O(n)\) 的(贪心+ ...