Codeforces 837E. Vasya's Function
题意:
- f(a, 0) = 0;
- f(a, b) = 1 + f(a, b - gcd(a, b))
- 输出f(a,b)
a=A*gcd(a,b) b=B*gcd(a,b)
一次递归后,变成了 f(A*gcd(a,b),(B-1)*gcd(a,b))
若gcd(A,(B-1))=1,那么 这一层递归的gcd(a,b)仍等于上一层递归的gcd(a,b)
也就是说,b-gcd(a,b),有大量的时间减的gcd(a,b)相同,
若计算出减了S次相同的gcd(a,b)
那就可以直接由f(a,b)到 f(a,b-S*gcd(a,b))+ S
设T是A的任意因子
那么S满足 (B-S)%T=0,且S最小
移项得 S=B%T
即S是B对A的所有因子取模得到的数中最小的那个
新的一次递归中,
a ' =a,b ' =(B-S)*gcd(a,b),gcd ' = gcd*T
如何得到T和S?
枚举所有因子会TLE
我们发现,整个过程a始终没有改变,只是参与了求gcd
对于输入的a,b
令A=a/gcd(a,b),B=b/gcd(a,b)
这样A,B 就没有公因子
这样 原来的b-S*gcd 相当于 现在的B-S
求出A的所有素因子
因为A为定值,所以下一次求S用A的素因子时,可以从上一次求S用的A的素因子剩下的里选
就是说
如果这次某个因子是B的因子,那么下一次就不会用这个因子了,B/=这个因子
即这个因子参与构成新的gcd
如果这次某个因子不是B的因子,下一次就可以考虑它
#include<vector>
#include<iostream>
using namespace std;
typedef long long LL;
LL ans;
vector<LL>p;
vector<LL> :: iterator it;
LL gcd(LL a,LL b) { return !b ? a:gcd(b,a%b); }
void work(LL y)
{
if(!y) return;
LL k=y;
for(it=p.begin();it!=p.end();++it) k=min(k,y%(*it));
ans+=k; y-=k;
vector<LL>q;
for(it=p.begin();it!=p.end();++it)
if(y%(*it)) q.push_back(*it);
else y/=(*it);
swap(p,q);
work(y);
}
int main()
{
LL a,b;
cin>>a>>b;
LL g=gcd(a,b);
a/=g; b/=g;
for(LL i=;i*i<=a;i++)
while(a%i==)
{
p.push_back(i);
a/=i;
}
if(a>) p.push_back(a);
work(b);
cout<<ans;
}
1 second
256 megabytes
standard input
standard output
Vasya is studying number theory. He has denoted a function f(a, b) such that:
- f(a, 0) = 0;
- f(a, b) = 1 + f(a, b - gcd(a, b)), where gcd(a, b) is the greatest common divisor of a and b.
Vasya has two numbers x and y, and he wants to calculate f(x, y). He tried to do it by himself, but found out that calculating this function the way he wants to do that might take very long time. So he decided to ask you to implement a program that will calculate this function swiftly.
The first line contains two integer numbers x and y (1 ≤ x, y ≤ 1012).
Print f(x, y).
3 5
3
6 3
1
Codeforces 837E. Vasya's Function的更多相关文章
- CodeForces - 837E - Vasya's Function | Educational Codeforces Round 26
/* CodeForces - 837E - Vasya's Function [ 数论 ] | Educational Codeforces Round 26 题意: f(a, 0) = 0; f( ...
- Codeforces 837E Vasya's Function - 数论
Vasya is studying number theory. He has denoted a function f(a, b) such that: f(a, 0) = 0; f(a, b) = ...
- Codeforces 837E Vasya's Function 数论 找规律
题意:定义F(a,0) = 0,F(a,b) = 1 + F(a,b - GCD(a,b).给定 x 和 y (<=1e12)求F(x,y). 题解:a=A*GCD(a,b) b=B*GCD(a ...
- 递推DP URAL 1353 Milliard Vasya's Function
题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...
- ural 1353. Milliard Vasya's Function(背包/递归深搜)
1353. Milliard Vasya's Function Time limit: 1.0 second Memory limit: 64 MB Vasya is the beginning ma ...
- ural 1353. Milliard Vasya's Function(dp)
1353. Milliard Vasya's Function Time limit: 1.0 second Memory limit: 64 MB Vasya is the beginning ma ...
- CodeForces 840A - Leha and Function | Codeforces Round #429 (Div. 1)
/* CodeForces 840A - Leha and Function [ 贪心 ] | Codeforces Round #429 (Div. 1) A越大,B越小,越好 */ #includ ...
- Vasya's Function CodeForces - 837E (gcd)
大意: 给定$a,b$, $1\le a,b\le 1e12$, 定义 $f(a,0)=0$ $f(a,b)=1+f(a,b-gcd(a,b))$ 求$f(a,b)$. 观察可以发现, 每次$b$一定 ...
- Codeforces 837 E Vasya's Function
Discription Vasya is studying number theory. He has denoted a function f(a, b) such that: f(a, 0) = ...
随机推荐
- POJ1743:Musical Theme
题目 vjudge Sol 先差分 然后求不可重叠最长重复子串 bits/stdc++.h会CE # include <bits/stdc++.h> # define IL inline ...
- 【HNOI2011】数学作业
分段矩乘即可 # include <stdio.h> # include <stdlib.h> # include <iostream> # include < ...
- 简简单单把event loop说清楚
event loop这东西,确实把我坑了一把,面试的时候被问到这个问题的时候,我是懵逼的,完全不知道怎么回答,而当我回来查到原来这个听起来如此玄乎又厉害的名字就是异步和单线程那块的知识的时候,我心情又 ...
- JavaScript中的execCommand
execCommand方法是执行一个对当前文档,当前选择或者给出范围的命令.处理Html数据时常用 如下格式:document.execCommand(sCommand[,交互方式, 动态参数]) , ...
- 转: web 页面加载速度优化实战-100% 的飞跃提升
前言 一个网站的加载速度有多重要? 反正我相信之前来 博主网站 的人至少有 50% 在加载完成前关闭了本站. 为啥捏? 看图 首页完整加载时间 8.18s,看来能进来看博主网站的人都是真爱呀,哈哈. ...
- js中判定this的规则
判定this new绑定:新建对象; var bar = new foo(); 明确绑定(call.apply,bind):指定对象; var bar = foo.call(obj) 隐含绑定:环境对 ...
- 初尝Eclipse
一.选择开发工具 1.Eclipse和JDK 我所选用的是Eclipse开发工具,此工具可以用来编译JAVA语言,但windows系统没有内置的JAVA运行环境,所以需要下载JDK,用来配置JAVA的 ...
- 关于ASP.NET MVC的js和css资源管理
本文来源于博客园-钱智慧,转载请注明出处 通过这篇文章和这个回答,我们总结如下: 浏览器针对一个域名,最多只会开启6个线程来加载文件,比如head中如果有7个引入文件(js文件或者css文件)的标签, ...
- [ZJOI2007] 矩阵游戏
Description 小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏――矩阵游戏.矩阵游戏在一个N*N黑白方阵进行(如同国际象棋一般,只是颜色是随意的).每次可以对该矩阵进行两 ...
- 笔记:MyBatis 其他特性
多行结果集映射成Map 如果你有一个映射语句返回多行记录,并且你想以HashMap的形式存储记录的值,使用记录列名作为key值,而记录对应值或为value值.我们可以使用sqlSession.sele ...