http://codeforces.com/problemset/problem/595/A
It was easy realization problem. Let's increase the variable i from 1 to n, and inside let's increase the variable j from 1 to 2·m. On every iteration we will increase the variable j on 2. If on current iteration a[i][j] = '1' or a[i][j + 1] = '1' let's increase the answer on one.
Asymptotic behavior of this solution — O(nm).
题目大意:
有n层楼, 每层有2 * m个窗户, 每家有2个窗户, 任何一个窗户亮说明这家还未休息, 问你有几家没有休息。
#include
#include
using namespace std;
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
int x,cnt = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
int flag = 0;
for(int k = 0; k < 2; k++){
scanf("%d",&x);
if(x == 1)
flag = 1;
}
if(flag)
cnt++;
}
}
printf("%d\n",cnt);
}
return 0;
}
http://codeforces.com/problemset/problem/595/B
Let's calculate the answer to every block separately from each other and multiply the answer to the previous blocks to the answer for current block.
For the block with length equals to k we can calculate the answer in the following way. Let for this block the number must be divided on xand must not starts with digit y. Then the answer for this block — the number of numbers containing exactly k digits and which divisible byx, subtract the number of numbers which have the first digit equals to y and containing exactly k digits and plus the number of numbers which have the first digit equals to y - 1 (only if y > 0) and containing exactly k digits.
Asymptotic behavior of this solution — O(n / k).
题目大意:给出你n / k个a[i]和n / k个b[i], 问你有多少个k位数, 使得不以b[i]开头并且可以整除a[i]。
算法思想:
数学题,,容斥定理。 求出所有情况的总数, 减去以b[i]开头的数目就为答案。
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
ll num[15];
ll a[100005],b;
int main(){
num[0] = 1;
for(int i = 1; i <= 10; i++)
num[i] = num[i-1]*10;
int n,k;
while(~scanf("%d%d",&n,&k)){
int sum = n/k;
for(int i = 0; i < sum; i++)
scanf("%lld",&a[i]);
ll ans = 1;
for(int i = 0; i < sum; i++){
scanf("%lld",&b);
ll cnt = (num[k]-1)/a[i]+1;
ll l = b*num[k-1],r = (b+1)*num[k-1]-1;
if(l == 0)
cnt = cnt-(r/a[i]+1);
else
cnt = cnt -(r/a[i]-(l-1)/a[i]);
ans = (ans*cnt)%mod;
}
printf("%lld\n",ans);
}
return 0;
}