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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

2019牛客网暑假多校训练第四场 K —number

發布時間:2025/5/22 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2019牛客网暑假多校训练第四场 K —number 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

鏈接:https://ac.nowcoder.com/acm/contest/884/K
來源:牛客網

題目描述

300iq loves numbers who are multiple of 300. One day he got a string consisted of numbers. He wants to know how many substrings in the string are multiples of 300 when considered as decimal integers. Note that leading and trailing zeros are allowed (both in original string and substrings you chose)?and the same substring appearing in different places can be counted multiple times.

輸入描述:

A single line consisting a string consisted of characters '0' to '9'.

輸出描述:

The number of substrings that are multiples of 300 when considered as decimal integers.


題解:首先我們分析滿足條件的情況:能被300整除的子串
   1.串的最后兩位肯定為00
   2.前面的各位和肯定能整除以3
   
   所以我們可以 開一個變量 sum, 記錄下 ”前i位的和 mod 3 的情況“。 情況有三種:sum==0,sum==1,sum==2
   再開一個數組 cnt[],記錄下 ”前i為的和 mod 3 的情況 出現的次數“。也就是只記錄 cnt[0],cnt[1],cnt[2]
   
   為什么要記錄 1和2的情況呢?舉個例子:
   在[L,R]的區間中,如果 在L處的sum 和 在R處的sum相等的話,那么就意味著 L~R的數位和 mod 3 == 0.也就是[L,R]為一個能整除3的子串。
   
   
//#pragma comment(linker, "/STACK:1024000000,1024000000") //#pragma GCC optimize(2) //#include <bits/stdc++.h> #include <algorithm> #include <iostream> #include<fstream> #include<sstream> #include<iterator> #include<cstring> #include<string> #include<cmath> #include<cstdio> #include<cctype> #include<vector> #include<deque> #include<queue> #include<stack> #include<map> #include<list> #include<set>using namespace std; typedef double dou; typedef long long ll; typedef pair<int, int> pii; typedef map<int, int> mii;#define pai acos(-1.0) #define M 100005 #define inf 0x3f3f3f3f #define mod 1000000007 #define left k<<1 #define right k<<1|1 #define lson L, mid, left #define rson mid + 1, R, right #define W(a) while(a) #define ms(a,b) memset(a,b,sizeof(a)) #define Abs(a) (a ^ (a >> 31)) - (a >> 31)char str[M]; int cnt[M];int main() {ios::sync_with_stdio(false);cin.tie(nullptr), cout.tie(nullptr);W (cin>>str) { ll ans = 0;int len = strlen(str);for (int i = 0; i < len; i++) {if (str[i] == '0') ans++;//先統計單個0的情況 }cnt[0] = 1;int sum = 0;//前i位mod 3的值for (int i = 0; i < len; i++) {sum += str[i] - '0';sum %= 3;if (i + 1 < len && str[i] == '0' && str[i + 1] == '0') {//滿足尾數為00 且在長度范圍內ans += cnt[sum];}cnt[sum]++;//cnt[0] 、cnt[1]、cnt[2] }cout << ans << endl;}return 0; }

?




轉載于:https://www.cnblogs.com/caibingxu/p/11258190.html

總結

以上是生活随笔為你收集整理的2019牛客网暑假多校训练第四场 K —number的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。