ural Infernal Work

Infernal Work

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

Railwaymen Vassily and Pyotr died and were sent to Hell. Their first punishment was to perform a complete inspection of the Moscow–Vladivostok railroad. They spent many weeks walking along the railroad together, one of them along the left rail and the other along the right rail, writing the long serial numbers of ties to their thick notebooks. As soon as they finished that infernal task, they immediately got a new task, which was even more meaningless. Now they had to count the number of pairs of ties that were written in Vassily‘s notebook on the same page and in Pyotr‘s notebook on different pages.

The friends came to you in a dream and asked you to save them from that terrible torment.

Input

The only input line contains integers abn (1 ≤ ab ≤ n ≤ 25 000 000). One page in Vassily‘s notebook comprises a numbers of ties, and one page in Pyotr‘s notebook comprises b numbers of ties. They have written numbers of n ties. All these numbers are different and are written in their notebooks in the same order.

Output

Output one number, which is the answer to the problem.

Sample Input

input output
3 4 10
4
2 4 10
0

Notes

Let the ties in the first sample be numbered by the letters from A to J. Then the following four pairs satisfy the condition: (D, E), (D, F), (G, I), (H, I).

首先看第一种方法(超出空间限制):

 1 #include <iostream>
 2 using namespace std;
 3
 4 struct Data{
 5     long long  zua;
 6     long long  zub;
 7 };
 8 //本题的思路是为两种不同的分组方式分别赋予不同的组别值,
 9 //然后搜索符合题目要求的配对。
10 int main(){
11     long long  a,b,n;
12     cin>>a>>b>>n;
13     Data data[n];
14
15     long long num1=1,num2=1;
16     for(long long  i=0;i<n;i++){
17         data[i].zua=num1;
18         data[i].zub=num2;
19         if((i+1)%a==0){
20             ++num1;
21         }
22         if((i+1)%b==0){
23             ++num2;
24         }
25     }
26     long long  result=0;
27     for(long long  i=0;i<n;i++){
28         for(long long  j=i;j<n&&j<i+a;j++){
29             if(data[i].zua==data[j].zua&&data[i].zub!=data[j].zub){
30                 ++result;
31             }
32         }
33     }
34     cout<<result;
35 }
36  

那么就用一个数组来实现它:

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5
 6 int main(){
 7     int a,b,n;
 8     cin>>a>>b>>n;
 9     long long result=0;
10     bool qwe;
11     for(int i=0;i<n;i=i+a){
12         int yb=i/b,k=1,cur;
13         qwe=0;
14
15         for(int j=i+1;j<i+a&&j<n;j++){
16             int yb2=j/b;
17             if(yb2==yb) continue;
18             if(!qwe){
19                 cur=yb2;
20                 k=j-i;
21                 qwe=1;
22             }
23             if(yb2!=cur){
24                 k=j-i;
25                 cur=yb2;
26             }
27             result+=k;
28         }
29     }
30     cout<<result<<endl;
31 }
时间: 2024-11-15 04:41:27

ural Infernal Work的相关文章

Ural 1081 Binary Lexicographic Sequence(DP)

题目地址:Ural 1081 先用dp求出每个长度下的合法序列(开头为1)的个数.然后求前缀和.会发现正好是一个斐波那契数列.然后每次判断是否大于此时长度下的最少个数,若大于,说明这一位肯定是1,若小于,则肯定是0.就这样不断输出出来即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #in

URAL 1684. Jack&#39;s Last Word KMP

题目来源:URAL 1684. Jack's Last Word 题意:输入a b 把b分成若干段 每一段都是a的前缀 思路:b为主串 然后用a匹配b 记录到b的i位置最大匹配的长度 然后分割 分割的时候要从后往前 如果a = abac b = abab 那么如果从前往后 首先覆盖了aba 然后b就不能覆盖了 从后往前就可以了 首先覆盖ab 下一次还是ab 因为已经记录了到i位置的最大匹配长度 根据长度从末尾倒退 每次倒退的时候只要是最大的匹配的长度 因为如果在某一次的递推 记录的最大匹配的前缀

ural 1272. Non-Yekaterinburg Subway

1272. Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to construct a subway. The peculiarity of the town is that it is located on small islands, some of them are connected with tunnels or bridges. The mayor is

ural 1273. Tie

1273. Tie Time limit: 1.0 secondMemory limit: 64 MB The subway constructors are not angels. The work under the ground and… Well, they are not angels. And where have you seen angels? It is all in a lifetime! Show me first somebody who has never… and t

ural 1269. Obscene Words Filter

1269. Obscene Words Filter Time limit: 0.5 secondMemory limit: 8 MB There is a problem to check messages of web-board visitors for the obscene words. Your elder colleagues commit this problem to you. You are to write a program, which check if there i

ural 1218. Episode N-th: The Jedi Tournament

1218. Episode N-th: The Jedi Tournament Time limit: 1.0 secondMemory limit: 64 MB Decided several Jedi Knights to organize a tournament once. To know, accumulates who the largest amount of Force. Brought each Jedi his lightsaber with him to the tourn

ural 1217. Unlucky Tickets

1217. Unlucky Tickets Time limit: 1.0 secondMemory limit: 64 MB Strange people live in Moscow! Each time in the bus, getting a ticket with a 6-digit number, they try to sum up the first half of digits and the last half of digits. If these two sums ar

ural 1219. Symbolic Sequence

1219. Symbolic Sequence Time limit: 1.0 secondMemory limit: 64 MB Your program is to output a sequence of 1 000 000 lowercase Latin letters. This sequence should satisfy the following restrictions: Every letter occurs not more than 40 000 times in th

Ural 1091 Tmutarakan Exams

Tmutarakan Exams Time Limit: 1000ms Memory Limit: 16384KB This problem will be judged on Ural. Original ID: 109164-bit integer IO format: %lld      Java class name: (Any) University of New Tmutarakan trains the first-class specialists in mental arith