Hartals
Consider three political parties. Assume h1 = 3, h2 =
4 and h3 = 8 where hi is
the hartal parameter for party i ( i = 1, 2, 3). Now, we will simulate the behavior of these three parties for N = 14 days. One must always start the simulation on a Sunday
and assume that there will be no hartals on weekly holidays (on Fridays and Saturdays).
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | |
Days | ||||||||||||||
Su | Mo | Tu | We | Th | Fr | Sa | Su | Mo | Tu | We | Th | Fr | Sa | |
Party 1 | x | x | x | x | ||||||||||
Party 2 | x | x | x | |||||||||||
Party 3 | x | |||||||||||||
Hartals | 1 | 2 | 3 | 4 | 5 |
The simulation above shows that there will be exactly 5 hartals (on days 3, 4, 8, 9 and 12) in 14 days. There will be no hartal on day 6 since it is a Friday. Hence we lose 5 working days in 2 weeks.
In this problem, given the hartal parameters for several political parties and the value of N, your job is to determine the number of working days we lose in those N days.
Input
The first line of the input consists of a single integer T giving the number of test cases to follow.
The first line of each test case contains an integer N ( ) giving the number of days over which the simulation must be run. The next line contains another integer P( ) representing the number of political parties in this case. The ith of the next P lines contains a positive integer hi (which will never be a multiple of 7) giving thehartal parameter for party i ( ).
Output
For each test case in the input output the number of working days we lose. Each output must be on a separate line.
Sample Input
2 14 3 3 4 8 100 4 12 15 25 40
Sample Output
5 15題目大意:
給你一個連續的天數,在星期六和星期五是休息,罷工是沒用的。再給你P個組織,每個組織給你一個數字,表示在這個數字的倍數上此政黨會罷工。問你這些天內罷工的總天數。
解題思路:
暴力枚舉,注意,可能多個組織在同一天罷工,這隻算一天。
代碼:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
int t,num,p[110];
bool visited[5000];
cin>>t;
while(t--){
int cnt=0,nump;
cin>>num>>nump;
memset(visited,false,num+1);
for(int i=0;i<nump;i++) cin>>p[i];
for(int i=0;i<nump;i++){
for(int j=1;j<=num;j++){
if(j%p[i]==0&&j%7!=6&&j%7!=0&&!visited[j]){
cnt++;
visited[j]=true;
}
}
}
cout<<cnt<<endl;
}
return 0;
}