2019牛客网暑假多校训练第四场 K —number
生活随笔
收集整理的這篇文章主要介紹了
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑系统怎么镜像到u盘 电脑系统如何U盘
- 下一篇: 电脑开机自动进入biso怎么调 电脑开机