日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Codeforces Round #326 (Div. 2) B. Pasha and Phone C. Duff and Weight Lifting

發(fā)布時(shí)間:2025/3/8 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Codeforces Round #326 (Div. 2) B. Pasha and Phone C. Duff and Weight Lifting 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

                        B. Pasha and Phone
Pasha has recently bought a new phone jPager and started adding his friends' phone numbers there. Each phone number consists of exactly n digits.

Also Pasha has a number k and two sequences of length n?/?k (n is divisible by k) a1,?a2,?...,?an?/?k and b1,?b2,?...,?bn?/?k. Let's split the phone number into blocks of length k. The first block will be formed by digits from the phone number that are on positions 1, 2,..., k, the second block will be formed by digits from the phone number that are on positions k?+?1, k?+?2, ..., 2·k and so on. Pasha considers a phone number good, if the i-th block doesn't start from the digit bi and is divisible by ai if represented as an integer.

To represent the block of length k as an integer, let's write it out as a sequence c1, c2,...,ck. Then the integer is calculated as the result of the expression c1·10k?-?1?+?c2·10k?-?2?+?...?+?ck.

Pasha asks you to calculate the number of good phone numbers of length n, for the given k, ai and bi. As this number can be too big, print it modulo 109?+?7.

Input
The first line of the input contains two integers n and k (1?≤?n?≤?100?000, 1?≤?k?≤?min(n,?9)) — the length of all phone numbers and the length of each block, respectively. It is guaranteed that n is divisible by k.

The second line of the input contains n?/?k space-separated positive integers — sequence a1,?a2,?...,?an?/?k (1?≤?ai?<?10k).

The third line of the input contains n?/?k space-separated positive integers — sequence b1,?b2,?...,?bn?/?k (0?≤?bi?≤?9).

Output
Print a single integer — the number of good phone numbers of length n modulo 109?+?7.

Sample test(s)
input
6 2
38 56 49
7 3 4
output
8
input
8 2
1 22 3 44
5 4 3 2
output
32400


Note
In the first test sample good phone numbers are: 000000, 000098, 005600, 005698, 380000, 380098, 385600, 385698.

?

題意:將一個(gè)長(zhǎng)度為n的phone number,分成長(zhǎng)度為k的n/k給塊,如果任意一個(gè)塊(塊i)中的數(shù)字x的開頭數(shù)字不是b[i], 且x可以整除a[i],那么就稱這個(gè)phone number是 good number。問這樣的good number 有多少個(gè)!

思路:容斥原理。 塊 i : tot = 數(shù)字長(zhǎng)度為k且可以整除a[i]的個(gè)數(shù);

?bi_tot = 數(shù)字長(zhǎng)度為k且開頭數(shù)字為b[i]且可以整除a[i]的個(gè)數(shù) -?數(shù)字長(zhǎng)度為k且開頭數(shù)字為(b[i]-1)且可以整除a[i]的個(gè)數(shù)

那么符合要求的數(shù)字個(gè)數(shù) = tot - bi_tot;(b[i]>0)

#include<iostream> #include<cstring> #include<cstdio> #include<cmath> #include<algorithm> #include<cstring> #define N 1000005 #define MOD 1000000007 using namespace std; typedef __int64 LL; int a[N]; int b[N]; int f[15]; void init(){f[0] = 1;for(LL i=1; i<10; ++i)f[i] = f[i-1] * 10; }int main(){init(); int n, k;scanf("%d%d", &n, &k);int m = n/k;for(int i=1; i<=m; ++i)scanf("%d", &a[i]);for(int i=1; i<=m; ++i)scanf("%d", &b[i]);LL ans = 1;for(int i=1; i<=m; ++i){LL tot = (f[k]-1)/a[i] + 1;LL bi_tot0 = (f[k-1]-1)/a[i] + 1;//block的開頭是0, 即b[i]==0 LL bi_tot = (f[k-1]*(b[i]+1)-1)/a[i] - (f[k-1]*b[i]-1)/a[i];//block的開頭不是0 if(b[i] == 0) ans *= tot-bi_tot0;else ans *= tot-bi_tot;ans %= MOD;}printf("%I64d\n", ans);return 0; }

?

C. Duff and Weight Lifting

Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of?i-th of them is?2^wi?pounds. In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimize the number of steps.

Duff is a competitive programming fan. That's why in each step, she can only lift and throw away a sequence of weights?2^a1,?...,?2^ak?if and only if there exists a non-negative integer?x?such that?2^a1?+?2^a2?+?...?+?2^ak?=?2^x, i. e. the sum of those numbers is a power of two.

Duff is a competitive programming fan, but not a programmer. That's why she asked for your help. Help her minimize the number of steps.

Input

The first line of input contains integer?n?(1?≤?n?≤?106), the number of weights.

The second line contains?n?integers?w1,?...,?wn?separated by spaces (0?≤?wi?≤?106?for each?1?≤?i?≤?n), the powers of two forming the weights values.

Output

Print the minimum number of steps in a single line.

Sample test(s) input 5
1 1 2 3 3 output 2 input 4
0 1 2 3 output 4 Note

In the first sample case: One optimal way would be to throw away the first three in the first step and the rest in the second step. Also, it's not possible to do it in one step because their sum is not a power of two.

In the second sample case: The only optimal way is to throw away one weight in each step. It's not possible to do it in less than 4 steps because there's no subset of weights with more than one weight and sum equal to a power of two.

題意:給定n個(gè)數(shù)w1, w2, w3,.......wn, 然后從這個(gè)n數(shù)中找出這樣的一個(gè)子序列a1,a2,a3....ak 使得 2^a1+2^a2.....+2^ak = 2^x, 然后可以刪除這個(gè)序列,

那么最少經(jīng)過幾步可以全部將這n個(gè)數(shù)刪除!

思路:其實(shí)就是 二進(jìn)制數(shù) 相加的過程,n個(gè)數(shù)對(duì)應(yīng)n個(gè)二進(jìn)制數(shù),從最低位到最高位相加,得到最后的二進(jìn)制數(shù)中 1 的個(gè)數(shù)就是答案!

#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<map> using namespace std; map<int, int, less<int> >mp; int main(){int n;scanf("%d", &n);while(n--){int x;scanf("%d", &x);mp[x]++;}int ans = 0;for(map<int,int>::iterator it = mp.begin(); it!=mp.end(); ++it){if(it->second>1)mp[it->first + 1] += it->second / 2;if(it->second%2 != 0) ++ans;}printf("%d\n", ans);return 0; }

?

轉(zhuǎn)載于:https://www.cnblogs.com/hujunzheng/p/4895835.html

總結(jié)

以上是生活随笔為你收集整理的Codeforces Round #326 (Div. 2) B. Pasha and Phone C. Duff and Weight Lifting的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。