Logo
(追記) (追記ここまで)

30484번 - Inversions 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
0.5 초 (추가 시간 없음) 1024 MB127565045.045%

문제

Every year, mathematicians and computer scientists from around the globe gather for the prestigious Inversion Counting Puzzle Contest (ICPC). For the next ICPC, the organizers had prepared the following challenge: given a string S consisting of lowercase letters, count the number of inversions in it. An inversion is a pair of indices i < j such that Si (the letter at position i) comes after Sj in the alphabet.

However, just last month, a group of outstanding researchers devised a sophisticated algorithm that can count the inversions in a string extremely fast. While this was great news for the advancement of science, it has been a nightmare for the ICPC staff, since their planned challenge is now obsolete.

This issue escalated to the head problem setter, who then presented a clever idea. Instead of simply receiving a string S, they should ask participants to repeat this string N times before counting the inversions. If the judges then set N to be large enough, at some point the algorithm proposed by the researchers will start to be too slow. Happy with this idea, the ICPC staff went ahead with organizing the next contest.

Unfortunately, now the judges don’t know the answers to the input files anymore, and therefore can’t judge submissions! The ICPC has just kicked off, and participants are about to start submitting their solutions. Please help the judges by computing the answers, so that the ICPC can run smoothly.

입력

The first line contains a string S (1 ≤ |S| ≤ 105), which is made of lowercase letters.

The second line contains an integer N (1 ≤ N ≤ 1012) indicating how many times the string S is to be repeated.

출력

Output a single line with an integer indicating the number of inversions in the string SN (S repeated N times). Because this number can be very large, output the remainder of dividing it by 109 + 7.

제한

예제 입력 1

ba
1

예제 출력 1

1

예제 입력 2

ab
3

예제 출력 2

3

예제 입력 3

zkba
1

예제 출력 3

6

예제 입력 4

cab
7

예제 출력 4

77

힌트

출처

ICPC > Regionals > Latin America > Latin America Regional Contests > Latin America Regional Contests 2023 I번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /