問題文
1
から 9
までの数字のみからなる文字列 SS の倍数である。
制約
- 1≤|S|≤2000001≤|S|≤200000 は
1
から9
までの数字のみからなる文字列
入力
入力は以下の形式で標準入力から与えられる。
SS
出力
条件を満たす整数の組 (i,j)(i,j)) の総数を出力せよ。
入力例 1 Copy
Copy
1817181712114
出力例 1 Copy
Copy
3
条件を満たすのは (1,5),(5,9),(9,13)(1,5),(5,9),(9,13) 個です。
入力例 2 Copy
Copy
14282668646
出力例 2 Copy
Copy
2
入力例 3 Copy
Copy
2119
出力例 3 Copy
Copy
0
条件を満たす整数の組は存在しません。
Score : 400400 points
Problem Statement
Given is a string SS.
Constraints
- 1≤|S|≤2000001≤|S|≤200000 is a string consisting of digits from
1
through9
.
Input
Input is given from Standard Input in the following format:
SS
Output
Print the number of pairs of integers (i,j)(i,j)) that satisfy the condition.
Sample Input 1 Copy
Copy
1817181712114
Sample Output 1 Copy
Copy
3
Three pairs - (1,5)(1,5) - satisfy the condition.
Sample Input 2 Copy
Copy
14282668646
Sample Output 2 Copy
Copy
2
Sample Input 3 Copy
Copy
2119
Sample Output 3 Copy
Copy
0
No pairs satisfy the condition.
**
题目意思:找区间为2019的倍数有多少个 题解:转化为(10+str[i])%2019 使用滚动数组 O(n2019)
**
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<set>
#include<vector>
#include<map>
#include<queue>
#define ll long long
using namespace std;
int dp[2][2020];
int main(){
string str;
ll ans=0;
cin>>str;
for(int i=0;i<str.length();i++)
{
for(int j=0;j<2019;j++)
{
dp[(i+1)%2][j]=0;
}
int k=str[i]-'0';
dp[(i+1)%2][k]=1;
for(int j=0;j<2019;j++)
{
dp[(i+1)%2][(j*10+k)%2019]+=dp[i%2][j];
}
ans+=dp[i%2][0];
}
ans+=dp[str.length()%2][0];
cout<<ans<<endl;
return 0;
}