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).
 
 
首先看第一种方法(超出空间限制):
 #include <iostream>
using namespace std; struct Data{
long long zua;
long long zub;
};
//本题的思路是为两种不同的分组方式分别赋予不同的组别值,
//然后搜索符合题目要求的配对。
int main(){
long long a,b,n;
cin>>a>>b>>n;
Data data[n]; long long num1=,num2=;
for(long long i=;i<n;i++){
data[i].zua=num1;
data[i].zub=num2;
if((i+)%a==){
++num1;
}
if((i+)%b==){
++num2;
}
}
long long result=;
for(long long i=;i<n;i++){
for(long long j=i;j<n&&j<i+a;j++){
if(data[i].zua==data[j].zua&&data[i].zub!=data[j].zub){
++result;
}
}
}
cout<<result;
}

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

 #include <iostream>
#include <cstring>
#include <algorithm>
using namespace std; int main(){
int a,b,n;
cin>>a>>b>>n;
long long result=;
bool qwe;
for(int i=;i<n;i=i+a){
int yb=i/b,k=,cur;
qwe=; for(int j=i+;j<i+a&&j<n;j++){
int yb2=j/b;
if(yb2==yb) continue;
if(!qwe){
cur=yb2;
k=j-i;
qwe=;
}
if(yb2!=cur){
k=j-i;
cur=yb2;
}
result+=k;
}
}
cout<<result<<endl;
}
 
 
 

ural Infernal Work的更多相关文章

  1. 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome

    题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...

  2. ural 2071. Juice Cocktails

    2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...

  3. ural 2073. Log Files

    2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...

  4. ural 2070. Interesting Numbers

    2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...

  5. ural 2069. Hard Rock

    2069. Hard Rock Time limit: 1.0 secondMemory limit: 64 MB Ilya is a frontman of the most famous rock ...

  6. ural 2068. Game of Nuts

    2068. Game of Nuts Time limit: 1.0 secondMemory limit: 64 MB The war for Westeros is still in proces ...

  7. ural 2067. Friends and Berries

    2067. Friends and Berries Time limit: 2.0 secondMemory limit: 64 MB There is a group of n children. ...

  8. ural 2066. Simple Expression

    2066. Simple Expression Time limit: 1.0 secondMemory limit: 64 MB You probably know that Alex is a v ...

  9. ural 2065. Different Sums

    2065. Different Sums Time limit: 1.0 secondMemory limit: 64 MB Alex is a very serious mathematician ...

随机推荐

  1. pqgrid对json数据的绑定

    $(function () { var data = [[1, 'Exxon Mobil', '339,938.0', '36,130.0'], [2, 'Wal-Mart Stores', '315 ...

  2. CSS的盒子模型

    1.边框:包裹内容的容器 统一设置:border: border-width border-style border-color 上:border-top 下:border-bottom 左:bord ...

  3. Chrome 控制台console的用法

    下面我们来看看console里面具体提供了哪些方法可以供我们平时调试时使用. 目前控制台方法和属性有: ["$$", "$x", "dir" ...

  4. ArcGIS中的WKID

    提到坐标系统,大家多少能明白一些,但在运用时,有些朋友搞得不是非常清楚,以后专门来总结.在实地生产项目中,使用较多的2000中国大地坐标系(CGCS2000).1980西安坐标系.1954北京坐标系统 ...

  5. CAGradientLayer渐变效果

    属性 startPoint和endPoint 决定渐变方向,以单位坐标系定义.左上角{0,0},右下角{1,1} colors 渐变的颜色,是一个CGColorRef的数组. locations 定义 ...

  6. 属性观察器willSet与didSet

    willSet与didSet是swift特有的,就是监听属性值的变化,但有一个小注意点. willSet与didSet调用时机:对象创建后的第一次赋值时开始调用.也就是说,初始化时(包含重载的init ...

  7. Service和Thread的关系及如何启用Service,如何停用Service

    Service和Thread的关系: 不少Android初学者都可能会有这样的疑惑,Service和Thread到底有什么关系呢?什么时候应该用Service,什么时候又应该用Thread?答案可能会 ...

  8. Android Adapter的几个方法

    1  ListView是在什么时候设置对Adapter的数据监听的? 在setAdapter(ListAdapter adapter)中,会先取消ListView中原来的mAdapter中的数据监听( ...

  9. Java 线程通信

    线程通信用来保证线程协调运行,一般在做线程同步的时候才需要考虑线程通信的问题. 1.传统的线程通信 通常利用Objeclt类提供的三个方法: wait() 导致当前线程等待,并释放该同步监视器的锁定, ...

  10. sshd安装

    centos yum install openssh-server #chkconfig --level 2345 sshd on #service sshd restart 重新启动 #netsta ...