【AtCoder Beginner Contest 164】D - Multiple of 2019

题目链接

問題文

1 から 9 までの数字のみからなる文字列 SS の倍数である。

制約

  • 1|S|2000001≤|S|≤2000001 から 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 through 9.

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值